You are here

function workflow_form_alter in Workflow 5.2

Same name and namespace in other branches
  1. 8 workflow.form.inc \workflow_form_alter()
  2. 5 workflow.module \workflow_form_alter()
  3. 6.2 workflow.module \workflow_form_alter()
  4. 6 workflow.module \workflow_form_alter()
  5. 7.2 workflow.form.inc \workflow_form_alter()

Modify the node form to add the workflow field.

File

./workflow.module, line 509

Code

function workflow_form_alter($form_id, &$form) {

  // Ignore all forms except comment forms and node editing forms.
  if ($form_id == 'comment_form' || isset($form['type']) && $form['#base'] == 'node_form') {
    if (isset($form['#node'])) {
      $node = $form['#node'];

      // Abort if user does not want to display workflow form on node editing form.
      if (!in_array('node', variable_get('workflow_' . $node->type, array(
        'node',
      )))) {
        return;
      }
    }
    else {
      $type = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $form['nid']['#value']));

      // Abort if user does not want to display workflow form on node editing form.
      if (!in_array('comment', variable_get('workflow_' . $type, array(
        'node',
      )))) {
        return;
      }
      $node = node_load($form['nid']['#value']);
    }
    $choices = workflow_field_choices($node);
    $wid = workflow_get_workflow_for_type($node->type);
    $states = workflow_get_states($wid);
    $current = workflow_node_current_state($node);
    $min = $states[$current] == t('(creation)') ? 1 : 2;

    // Bail out if user has no new target state(s).
    if (count($choices) < $min) {
      return;
    }
    $name = check_plain(t(workflow_get_name($wid)));

    // If the current node state is not one of the choices, autoselect first choice.
    // We know all states in $choices are states that user has permission to
    // go to because workflow_field_choices() has already checked that.
    if (!isset($choices[$current])) {
      $array = array_keys($choices);
      $current = $array[0];
    }
    if (sizeof($choices) > 1) {
      $form['workflow'] = array(
        '#type' => 'fieldset',
        '#title' => $name,
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#weight' => 10,
      );
    }

    // See if scheduling information is present.
    if ($node->_workflow_scheduled_timestamp && $node->_workflow_scheduled_sid) {
      global $user;
      if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
        $timezone = $user->timezone;
      }
      else {
        $timezone = variable_get('date_default_timezone', 0);
      }

      // The default value should be the upcoming sid.
      $current = $node->_workflow_scheduled_sid;
      $timestamp = $node->_workflow_scheduled_timestamp;
      $comment = $node->_workflow_scheduled_comment;
    }
    workflow_node_form($form, $name, $name, $current, $choices, $timestamp, $comment);
  }
}