You are here

function _workflow_extensions_replace_with_buttons in Workflow Extensions 7

Same name and namespace in other branches
  1. 6 workflow_extensions.module \_workflow_extensions_replace_with_buttons()
1 call to _workflow_extensions_replace_with_buttons()
workflow_extensions_form_alter in ./workflow_extensions.module
Implements hook_form_alter().

File

./workflow_extensions.module, line 336
UI-related improvements to the Workflow module and tokens for Rules.

Code

function _workflow_extensions_replace_with_buttons(&$form, $workflow_name) {
  $current_sid = $form['workflow'][$workflow_name]['#default_value'];
  $current_state_name = workflow_get_workflow_states_by_sid($current_sid)->state;

  // We need a node-context for token replacement. When on the Workflow tab
  // form, the node object will already have been loaded on the form.
  // When creating content (node/add/<type>) we only have limited data. In
  // the remaining cases we load the node from the cache based on the nid
  // found on the form.
  $form_id = $form['form_id']['#value'];
  if (strpos($form_id, 'workflow_tab_form') === 0) {
    $node = $form['node']['#value'];
  }
  elseif (is_numeric($nid = $form['nid']['#value'])) {
    $node = node_load($nid);
  }
  else {

    // Creating new content, nid not yet known
    $node = $form['#node'];
  }
  $states = $form['workflow'][$workflow_name]['#options'];
  $submit_handlers = _workflow_extensions_assign_handlers($form);
  $form['workflow']['buttons'] = array(
    '#prefix' => '<div class="workflow-buttons">',
    '#suffix' => '</div>',
  );
  foreach ($states as $sid => $to_state_name) {
    if ($sid != $current_sid) {

      // Create button for transition from current_sid to destination state.
      $button = array();
      $button['#value'] = workflow_extensions_get_transition_label($form['#wf']->wid, $current_state_name, workflow_get_workflow_states_by_sid($sid)->state, $node);
      $button['#type'] = 'submit';
      $button['#to_state'] = $sid;
      if (isset($form['buttons']['submit']['#weight'])) {

        // node form
        $button['#weight'] = $form['buttons']['submit']['#weight'] + 1;
      }
      elseif (isset($form['submit']['#weight'])) {

        // comment form
        $button['#weight'] = $form['submit']['#weight'];
      }
      $button['#submit'] = $submit_handlers;
      $form['workflow']['buttons']["submit_to_{$to_state_name}"] = $button;
    }
  }
  $form['workflow'][$workflow_name]['#type'] = 'markup';
  $form['workflow'][$workflow_name]['#markup'] = '<h3>' . $form['workflow'][$workflow_name]['#title'] . '</h3>';

  // With the existing Save button now impotent to submit a workflow
  // transition, we can re-purpose it for saving all other edits to the
  // node without changing the workflow state.
  // This does not make sense for the Workflow tab form though, as there is
  // nothing to save but a state change. In this case we simply remove the
  // Save button.
  if ($form_id == 'comment_form') {
    $form['actions']['submit'] = $form['submit'];

    // ???
    $form['actions']['submit']['#submit'] = $form['#submit'];
    $form['actions']['submit']['#weight']--;

    // left-most
  }
  if (strpos($form_id, 'workflow_tab_form') !== 0 && ($label = variable_get('workflow_extensions_default_save_button_label', ''))) {
    $form['actions']['submit']['#value'] = workflow_extensions_replace_state_name_tokens($label, $current_state_name);
  }
  unset($form['submit']);

  // remove submit button...

  //unset($form['#submit']); // ... but don't remove handler, [#1097328]
}