User login

Convert Workflow's radio buttons to submit buttons in Drupal 6

Overview

When really messing with how a node and all the modules acting on it submit data when saving a node, you need to tackle things from both the form_alter side and the nodeapi side (or otherwise mess with the node submission).

Workflow uses nodeapi, and the only thing it's really looking for when inserting or updating a node is $node->workflow. We can set this in hook_nodeapi, most sensibly in op presave, or we can do it the probably harder but more correct-seeming way of an extra submit function. Detailed eventually below.

In this case we are transforming (some of) the radio buttons that Workflow module puts in based on your configuration of workflow (and we are trashing others, maybe I should see instead if they can be configured out of existence).

node_form_submit (in node.page.inc) calls node_form_submit_build_node(), also defined in node.page.inc, and calls node_submit() and hands it the values part of the $form_state variable after dumping any submit handlers. In node_submit() the node is made into an object (so the form field arrays become object properties) and has a few default things added.

More importantly, node_form_submit now calls node_save() on this node object. It is node_save(), defined in node.module, where nodeapi is called for presave and for insert or update.

You can look at what button was actually pressed by looking at op

<?php
   'submit' => 'Save Draft & Preview',
   'preview_changes' => 'Preview changes',
   'op' => 'Publish',
   'workflow_8' => 'Publish',
   'workflow_9' => 'Unpublish',
?>

Considering doing it in an additional form submit function added to all our new buttons. It seems cleaner that way because there's a specific array dedicated to it. And then in normal Drupal fashion there's an array with every piece of information it can think of throwing in there again.

Here it is:

<?php
array(
//...
  'clicked_button' =>
  array (
    '#type' => 'submit',
    '#access' => true,
    '#submit' =>
    array (
      0 => 'node_form_submit',
      1 => 'pdonline_workflow_node_submit',
    ),
    '#value' => 'Publish',
    '#post' =>
    array (
      'title' => 'yada yada',
      'field_author' =>
      array (
        0 =>
        array (
          'uid' =>
          array (
            'uid' => 'Albert Leentjens',
          ),
          '_weight' => '0',
        ),
        1 =>
        array (
          'uid' =>
          array (
            'uid' => '',
          ),
          '_weight' => '1',
        ),
      ),
      'body' => '',
      'field_citation' =>
      array (
        0 =>
        array (
          'nid' =>
          array (
            'nid' => '',
          ),
          '_weight' => '0',
        ),
        1 =>
        array (
          'nid' =>
          array (
            'nid' => '',
          ),
          '_weight' => '1',
        ),
      ),
      'changed' => '1240709036',
      'form_build_id' => 'form-2b5ef139d57dd30f94f08734b0bc932d',
      'form_token' => 'e61f4ad27931bc65d5626059ee149fe0',
      'form_id' => 'article_node_form',
      'search_block_set' => '',
      'workflow' => '9',
      'workflow_comment' => '',
      'revision_information' => '',
      'path' => '',
      'op' => 'Publish',
    ),
    '#programmed' => false,
    '#tree' => false,
    '#parents' =>
    array (
      0 => 'workflow_8',
    ),
    '#array_parents' =>
    array (
      0 => 'buttons',
      1 => 'workflow_8',
    ),
    '#weight' => 0.002,
    '#processed' => false,
    '#description' => NULL,
    '#attributes' =>
    array (
    ),
    '#required' => false,
    '#input' => true,
    '#name' => 'op',
    '#button_type' => 'submit',
    '#executes_submit_callback' => true,
    '#process' =>
    array (
      0 => 'form_expand_ahah',
    ),
    '#id' => 'edit-workflow-8',
  ),
  'redirect' => 'node/2448',
  'node' =>
  array (
    'nid' => '2448',
    'vid' => '5516',
    'uid' => '88',
    'created' => 1240708883,
    'type' => 'article',
    'language' => '',
    'changed' => '1240709036',
    'title' => 'yada yada',
    'body' => '',
    'format' => '5',
    'revision_information' => NULL,
    'revision' => true,
    'log' => '',
    'name' => 'testEditor',
    'date' => '2009-04-25 21:21:23 -0400',
    'status' => '1',
    'promote' => '0',
    'sticky' => '0',
    'submit' => 'Save Draft & Preview',
    'preview_changes' => 'Preview changes',
    'op' => 'Publish',
    'workflow_8' => 'Publish',
    'workflow_9' => 'Unpublish',
    'form_build_id' => 'form-2b5ef139d57dd30f94f08734b0bc932d',
    'form_token' => 'e61f4ad27931bc65d5626059ee149fe0',
    'form_id' => 'article_node_form',
// ... stuff cut ...
    'workflow' => '9',
    'workflow_comment' => '',
    'notifications_content_disable' => 0,
    'teaser' => '',
    'validated' => true,
  ),
  'nid' => '2448',
)
?>

Still with me? Good. The actual name of the array you just set your button to within the button array, which is the surest way of knowing exactly what button was pressed, since the value can be translated and I don't feel like working backwards through that curious land.

I was wrong in this old code comment while in the process of figuring this out:

The unfortunate thing about submit buttons is that they cary no information with them except the value-- and the value is what is visible to people.
So we'll use the workflow tables to match everything up. If you want different labels on the buttons, hack it with local translations in settings.php or, better and more easily, just change the name of the workflow state in Workflow's settings!
Would it be reasonable to put t() around this actually?
I was wrong, the information is in #parents

OK, that last part is true.

Resolution

Searched words: 
drupal convert workflow radio buttons to submit change workflow radio buttons submit drupal

Comments

Interesting, any chance of

Interesting, any chance of some more complete code samples ?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.