You are here

function workflow_node_form in Workflow 7

Same name and namespace in other branches
  1. 5.2 workflow.module \workflow_node_form()
  2. 5 workflow.module \workflow_node_form()
  3. 6.2 workflow.module \workflow_node_form()
  4. 6 workflow.module \workflow_node_form()

Form builder. Add form widgets for workflow change to $form.

This builder is factored out of workflow_form_alter() because it is also used on the Workflow tab.

Parameters

$form: An existing form definition array.

$name: The name of the workflow.

$current: The state ID of the current state, used as the default value.

$choices: An array of possible target states.

2 calls to workflow_node_form()
workflow_tab_form in ./workflow.pages.inc
Form builder. Allow workflow state change and scheduling from workflow tab. N.B. This function is only used for Node API, not Field API.
_workflow_form_alter in ./workflow.node.inc
Used to Implement hook_form_alter(). Is now a subfunction of workflow_form_BASE_FORM_ID_alter(). This is more performant, since it is called only on form with correct BASE_FORM_ID.

File

./workflow.module, line 280
Support workflows made up of arbitrary states.

Code

function workflow_node_form(&$form, $form_state, $title, $name, $current, $choices, $timestamp = NULL, $comment = NULL) {

  // Give form_alters the chance to see the parameters.
  $form['#wf_options'] = array(
    'title' => $title,
    'name' => $name,
    'current' => $current,
    'choices' => $choices,
    'timestamp' => $timestamp,
    'comment' => $comment,
  );
  if (count($choices) == 1) {

    // There is no need to show the single choice.
    // A form choice would be an array with the key of the state.
    $state = key($choices);
    $form['workflow'][$name] = array(
      '#type' => 'value',
      '#value' => array(
        $state => $state,
      ),
    );
  }
  else {
    $form['workflow'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'workflow-form-container',
        ),
      ),
    );

    // Note: title needs to be sanitized before calling this function.
    $form['workflow'][$name] = array(
      '#type' => 'radios',
      '#title' => !empty($form['#wf']->options['name_as_title']) ? $title : '',
      '#options' => $choices,
      '#name' => $name,
      '#parents' => array(
        'workflow',
      ),
      '#default_value' => $current,
    );
  }

  // Display scheduling form only if a node is being edited and user has
  // permission. State change cannot be scheduled at node creation because
  // that leaves the node in the (creation) state.
  if (!(arg(0) == 'node' && arg(1) == 'add') && (!isset($form['#wf']->options['schedule']) || !empty($form['#wf']->options['schedule'])) && user_access('schedule workflow transitions')) {
    $scheduled = $timestamp ? 1 : 0;
    $timestamp = $scheduled ? $timestamp : REQUEST_TIME;
    $form['workflow']['workflow_scheduled'] = array(
      '#type' => 'radios',
      '#title' => t('Schedule'),
      '#options' => array(
        t('Immediately'),
        t('Schedule for state change'),
      ),
      '#default_value' => isset($form_state['values']['workflow_scheduled']) ? $form_state['values']['workflow_scheduled'] : $scheduled,
    );
    $form['workflow']['workflow_scheduled_date_time'] = array(
      '#type' => 'fieldset',
      '#title' => t('At'),
      '#prefix' => '<div style="margin-left: 1em;">',
      '#suffix' => '</div>',
      '#states' => array(
        'visible' => array(
          ':input[name="workflow_scheduled"]' => array(
            'value' => 1,
          ),
        ),
        'invisible' => array(
          ':input[name="workflow_scheduled"]' => array(
            'value' => 0,
          ),
        ),
      ),
    );
    $form['workflow']['workflow_scheduled_date_time']['workflow_scheduled_date'] = array(
      '#type' => 'date',
      '#default_value' => array(
        'day' => isset($form_state['values']['workflow_scheduled_date']['day']) ? $form_state['values']['workflow_scheduled_date']['day'] : format_date($timestamp, 'custom', 'j'),
        'month' => isset($form_state['values']['workflow_scheduled_date']['month']) ? $form_state['values']['workflow_scheduled_date']['month'] : format_date($timestamp, 'custom', 'n'),
        'year' => isset($form_state['values']['workflow_scheduled_date']['year']) ? $form_state['values']['workflow_scheduled_date']['year'] : format_date($timestamp, 'custom', 'Y'),
      ),
    );
    $hours = format_date($timestamp, 'custom', 'H:i');
    $form['workflow']['workflow_scheduled_date_time']['workflow_scheduled_hour'] = array(
      '#type' => 'textfield',
      '#description' => t('Please enter a time in 24 hour (e.g. HH:MM) format.
          If no time is included, the default will be midnight on the specified date.
          The current time is: @time', array(
        '@time' => $hours,
      )),
      '#default_value' => $scheduled ? isset($form_state['values']['workflow_scheduled_hour']) ? $form_state['values']['workflow_scheduled_hour'] : $hours : '00:00',
    );
    global $user;
    if (variable_get('configurable_timezones', 1) && $user->uid && drupal_strlen($user->timezone)) {
      $timezone = $user->timezone;
    }
    else {
      $timezone = variable_get('date_default_timezone', 0);
    }
    $timezone_type = isset($form['#wf']->options['schedule_timezone']) && empty($form['#wf']->options['schedule_timezone']) ? 'hidden' : 'select';
    $timezones = drupal_map_assoc(timezone_identifiers_list());
    $form['workflow']['workflow_scheduled_date_time']['workflow_scheduled_timezone'] = array(
      '#type' => $timezone_type,
      '#title' => t('Time zone'),
      '#options' => $timezones,
      '#default_value' => array(
        $timezone => $timezone,
      ),
    );
  }

  // Determine if the Comment field must be shown.
  // This does not work if a node type has both Node and Field enabled.
  $determiner = isset($form['#tab']) ? 'comment_log_tab' : 'comment_log_node';
  $comment_type = 'hidden';
  if (isset($form['#wf']->options[$determiner])) {
    $comment_type = $form['#wf']->options[$determiner] ? 'textarea' : 'hidden';
  }
  $form['workflow']['workflow_comment'] = array(
    '#type' => $comment_type,
    '#title' => t('Workflow comment'),
    '#description' => t('A comment to put in the workflow log.'),
    '#default_value' => $comment,
    '#rows' => 2,
  );
}