You are here

function workflow_extensions_form_alter in Workflow Extensions 6

Same name and namespace in other branches
  1. 7 workflow_extensions.module \workflow_extensions_form_alter()

Implementation of hook_form_alter().

1) Remove the Workflow radio buttons and replace each state transition by a submit button with a configurable, explanatory label. To allow saving of edits to the node without a state transition, display an additional button "Save, don't submit" (or similar). 2) If on the create/edit form suppress the workflow fieldset if the user does not have the relevant permission 3) If the schedule state transition form is enabled, suppress it until the user clicks the radio button to reveal it. 4) Add an (extra) form validator

File

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

Code

function workflow_extensions_form_alter(&$form, &$form_state, $form_id) {

  // If there's no Workflow fieldset, then we have nothing further to do.
  if (!isset($form['#wf'])) {

    // was $form['workflow'] but need to exclude content type edit form
    return;
  }

  // 4) Add form validator
  if (empty($form['#validate'])) {
    $form['#validate'] = array();
  }
  elseif (!is_array($form['#validate'])) {
    $form['#validate'] = array(
      $form['#validate'],
    );
  }
  $form['#validate'][] = 'workflow_extensions_workflow_form_validate';

  // 2) Suppress Workflow fieldset on the create/edit form if not permitted.
  if (isset($form['#id']) && $form['#id'] == 'node-form' && !user_access('change workflow state via node edit form')) {
    unset($form['workflow']);
    return;
  }

  // The next if-statement implements point 3) above
  if (variable_get('workflow_extensions_display_schedule_toggle', TRUE)) {
    $path = drupal_get_path('module', 'workflow_extensions');
    drupal_add_js($path . '/workflow_schedule.js', 'module');
  }

  // The rest of this function implements point 1)
  $workflow_name = workflow_extensions_extract_workflow_name($form);
  $workflow_radios = $form['workflow'][$workflow_name];
  if (is_array($workflow_radios) && isset($workflow_radios['#options'])) {

    // Use the form to work out the potential state transitions for this user.
    $states = $workflow_radios['#options'];

    // $states will be empty when creating node
    if (!empty($states)) {
      $title = variable_get('workflow_extensions_change_state_form_title', '');
      if (!empty($title)) {
        if (trim($title) == '<none>') {
          unset($form['workflow'][$workflow_name]['#title']);
        }
        else {
          $form['workflow'][$workflow_name]['#title'] = workflow_extensions_replace_tokens_raw($title);
        }
      }
      if (empty($form['#submit'])) {

        // This seems to happen as a side-effect of using hook_forms(), i.e
        // the case where workflow_extensions_change_state_form() is called,
        // for instance when the state change form appears in a block or View.
        // In the latter case, we also create ourselves an option to redirect
        // to page different from the default (which is node/%), as set in
        // workflow_tab_form_submit().
        $form['#submit'] = array(
          'workflow_tab_form_submit',
          'workflow_extensions_form_redirect',
        );
      }
      switch (variable_get('workflow_extensions_ui_style', WORKFLOW_EXTENSIONS_UI_BUTTONS)) {
        case WORKFLOW_EXTENSIONS_UI_BUTTONS:
          if (count($states) > 1) {
            _workflow_extensions_replace_with_buttons($form, $workflow_name);
          }
          break;
        case WORKFLOW_EXTENSIONS_UI_DROPDOWN:
          _workflow_extensions_replace_with_dropdown($form, $workflow_name);

        // no break;
        default:

          // radios
          if (isset($form['submit'])) {
            $submit_label = variable_get('workflow_extensions_change_state_button_label', '');
            if (!empty($submit_label)) {
              $form['submit']['#value'] = workflow_extensions_replace_tokens_raw($submit_label);
            }
          }
          break;
      }
    }
  }
}