You are here

function workbench_moderation_form_node_form_alter in Workbench Moderation 7.3

Same name and namespace in other branches
  1. 7 workbench_moderation.module \workbench_moderation_form_node_form_alter()
  2. 7.2 workbench_moderation.module \workbench_moderation_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

Forcing new reversion and publishing.

File

./workbench_moderation.module, line 926
Content moderation for Workbench.

Code

function workbench_moderation_form_node_form_alter(&$form, $form_state) {
  global $user;

  // This must be a node form and a node that has moderation enabled.
  // Extended to include moderation check on the individual node.
  if (!workbench_moderation_node_moderated($form['#node'])) {
    return;
  }

  // Set a moderation state even if there is not one defined
  if (isset($form['#node']->workbench_moderation['current']->state)) {
    $moderation_state = $form['#node']->workbench_moderation['current']->state;
  }
  else {
    $moderation_state = workbench_moderation_state_none();
  }

  // Store the current moderation state
  $form['workbench_moderation_state_current'] = array(
    '#type' => 'value',
    '#value' => $moderation_state,
  );

  // We have a use case where a published node is being edited. This will always
  // revert back to the original node status.
  if ($moderation_state == workbench_moderation_state_published()) {
    $moderation_state = workbench_moderation_state_none();
  }

  // Get all the states *this* user can access. If there aren't any, this user
  // can not change the moderation state.
  if ($states = workbench_moderation_states_next($moderation_state, $user, $form['#node'])) {
    $states[$moderation_state] = t('@state (Current)', array(
      '@state' => workbench_moderation_state_label($moderation_state),
    ));
    $states_sorted = array();
    foreach (array_keys(workbench_moderation_states()) as $state) {
      if (array_key_exists($state, $states)) {
        $states_sorted[$state] = $states[$state];
      }
    }
    $states = $states_sorted;
    $form['revision_information']['workbench_moderation_state_new'] = array(
      '#title' => t('Moderation state'),
      '#type' => 'select',
      '#options' => $states,
      '#description' => t('Set the moderation state for this content.'),
      '#access' => $states ? TRUE : FALSE,
    );

    // If the user has access to the pre-set default state, make it the default
    // here.  Otherwise, don't set a default in this case.
    $default_state = variable_get('workbench_moderation_default_state_' . $form['type']['#value']);
    if ($default_state && array_key_exists($default_state, $states)) {
      $form['revision_information']['workbench_moderation_state_new']['#default_value'] = $default_state;
    }
    else {
      $form['revision_information']['workbench_moderation_state_new']['#default_value'] = workbench_moderation_state_none();
    }
  }
  else {

    // Store the current moderation state
    $form['workbench_moderation_state_new'] = array(
      '#type' => 'value',
      '#value' => $moderation_state,
    );
  }

  // Always create new revisions for nodes that are moderated
  $form['revision_information']['revision'] = array(
    '#type' => 'value',
    '#value' => TRUE,
  );

  // Set a default revision log message.
  $logged_name = user_is_anonymous() ? variable_get('anonymous', t('Anonymous')) : format_username($user);
  if (!empty($form['#node']->nid)) {
    $form['revision_information']['log']['#default_value'] = t('Edited by !user.', array(
      '!user' => $logged_name,
    ));
  }
  else {
    $form['revision_information']['log']['#default_value'] = t('Created by !user.', array(
      '!user' => $logged_name,
    ));
  }

  // Move the revision log into the publishing options to make things pretty.
  if ($form['options']['#access']) {
    $form['options']['log'] = $form['revision_information']['log'];
    $form['options']['log']['#title'] = t('Moderation notes');
    $form['options']['workbench_moderation_state_new'] = isset($form['revision_information']['workbench_moderation_state_new']) ? $form['revision_information']['workbench_moderation_state_new'] : array();

    // Unset the old placement of the Revision log.
    unset($form['revision_information']['log']);
    unset($form['revision_information']['workbench_moderation_state_new']);

    // The revision information section should now be empty.
    $form['revision_information']['#access'] = FALSE;
  }

  // Setup the JS for the vertical tabs summary. The heavy weight allows this
  // script to replace the default node summary callbacks that get registered by
  // "lighter" scripts.
  // Note: Form API '#attached' does not allow to set a weight.
  drupal_add_js(drupal_get_path('module', 'workbench_moderation') . '/js/workbench_moderation.js', array(
    'weight' => 90,
  ));

  // Users can not choose to publish content; content can only be published by
  // setting the content's moderation state to "Published".
  $form['options']['status']['#access'] = FALSE;
  $form['actions']['submit']['#submit'][] = 'workbench_moderation_node_form_submit';
  workbench_moderation_messages('edit', $form['#node']);
}