You are here

function workflow_extensions_form_alter in Workflow Extensions 7

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

Implements 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 171
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';
  if (isset($form['workflow']['workflow_comment']) && !variable_get('workflow_extensions_allow_blank_comments', TRUE)) {
    $form['workflow']['workflow_comment']['#required'] = TRUE;
  }

  // 2) Suppress Workflow fieldset on the create/edit form if not permitted.
  if (isset($form['#node_edit_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');
  }

  // 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>') {
          $form['workflow'][$workflow_name]['#title'] = '';
        }
        else {
          $form['workflow'][$workflow_name]['#title'] = workflow_extensions_replace_tokens_raw($title);
        }
      }
      if (empty($form['#submit'])) {
        @(include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'workflow') . '/workflow.pages.inc');
        $form['#submit'] = array(
          'workflow_extensions_form_submit',
          'workflow_tab_form_submit',
        );
      }
      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:

          // dropdown and radios
          $form['#submit'][] = 'workflow_extensions_form_redirect';
          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;
      }
    }
  }
}