You are here

function workbench_access_node_form_element in Workbench Access 7

Provides a module-specific form for access control.

1 call to workbench_access_node_form_element()
workbench_access_form_alter in ./workbench_access.module
Implements hook_form_alter().

File

./workbench_access.module, line 1488
Workbench Access module file.

Code

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

  // Prepare the form element.
  $active = workbench_access_get_active_tree();
  if (empty($active['active'])) {
    drupal_set_message(workbench_access_sections_needed_message(), 'warning');
    return;
  }

  // Set default form options.
  $default = array();
  if (!empty($form['#node']->workbench_access)) {

    // In preview mode, this value can be a string.
    // See http://drupal.org/node/1935190.
    if (is_array($form['#node']->workbench_access)) {
      $current = array_keys($form['#node']->workbench_access);
    }
    else {
      $current = array(
        $form['#node']->workbench_access,
      );
    }
    foreach ($current as $access_id) {
      if (isset($active['active'][$access_id])) {
        $default[] = $access_id;
      }
    }
  }
  $options = array();
  $multiple = variable_get('workbench_access_allow_multiple', 0);

  // Force user selection if not set. See http://drupal.org/node/1348626.
  if (empty($multiple) && empty($default)) {
    $options[NULL] = t('- Select a value -');
  }

  // Get the user options.
  $options += workbench_access_active_options();

  // If there are no options and the 'workbench_access' variable has not been set
  // then it seems that Workbench Access has not been configured.
  if (empty($options) && !variable_get('workbench_access', FALSE)) {
    $message = workbench_access_configuration_needed_message();

    // Using 'error' instead of warning because the user should not have gotten this far
    // without configuring Workbench Access.
    drupal_set_message($message, 'error', FALSE);
  }

  // The base form element.
  $element = array(
    '#type' => 'select',
    '#title' => t('@message_label', array(
      '@message_label' => variable_get('workbench_access_label', 'Section'),
    )),
    '#options' => $options,
    '#required' => TRUE,
    '#default_value' => $default,
    '#multiple' => $multiple,
    '#description' => $multiple ? t('Select the proper editorial group(s) for this content.') : t('Select the proper editorial group for this content.'),
  );

  // If the default is set and is not in the user's range, then pass hidden and
  // display a message.
  // TODO: $default might legitimately be zero in some edge cases.
  if (!empty($default)) {
    $all = array();
    $disallowed = array();
    foreach ($default as $item) {
      if (isset($active['tree'][$item]['name'])) {
        $all[$active['tree'][$item]['access_id']] = $active['tree'][$item]['name'];
        if (!isset($options[$item])) {
          $disallowed[$active['tree'][$item]['access_id']] = $active['tree'][$item]['name'];
        }
      }
    }
    if (!empty($disallowed)) {
      $diff = array_diff($all, $disallowed);

      // TODO: If we toggle from single to multiple, then this can get messy.
      if (empty($diff) || !variable_get('workbench_access_allow_multiple', 0)) {
        $element['#type'] = 'value';
        $element['#value'] = $element['#default_value'];
        $form['workbench_access']['message'] = array(
          '#type' => 'item',
          '#title' => t('Workbench access'),
          '#markup' => t('%title is assigned to the %section editorial group(s), which may not be changed.', array(
            '%title' => $form['#node']->title,
            '%section' => implode(', ', $disallowed),
          )),
        );
      }
      else {
        $form['workbench_access']['workbench_access_fixed'] = array(
          '#type' => 'value',
          '#value' => array_keys($disallowed),
        );
        $element['#description'] = $element['#description'] . '<br />' . t('%title is also assigned to the %section editorial group(s), which may not be changed.', array(
          '%title' => $form['#node']->title,
          '%section' => implode(', ', $disallowed),
        ));
      }
    }
  }
  workbench_access_alter_form('node_element', $element, $form_state);
  $form['workbench_access']['workbench_access'] = $element;
}