You are here

function custom_pub_form_alter in Custom Publishing Options 6

Same name and namespace in other branches
  1. 7 custom_pub.module \custom_pub_form_alter()

Implements hook_form_alter().

File

./custom_pub.module, line 293
Adds the ability to add Custom publishing options to the node Add/Edit form.

Code

function custom_pub_form_alter(&$form, $form_state, $form_id) {
  $types = variable_get('custom_pub_types', array());

  //add the options to the node form
  if ($form['#id'] === 'node-form') {
    $node = $form['#node'];
    foreach ($types as $type) {
      if (!empty($type['node_types'][$node->type])) {
        $form['options'][$type['type']] = array(
          '#type' => 'checkbox',
          '#title' => check_plain(t($type['name'])),
          '#default_value' => $node->{$type}['type'],
          '#access' => user_access('administer nodes') ? TRUE : user_access('toggle ' . $type['type'] . ' custom options'),
        );
      }
    }
  }

  //Add options to the node_type_form for editing default node options
  if ($form_id == 'node_type_form') {
    $node_type = $form['#node_type'];
    foreach ($types as $type) {
      if (!empty($type['node_types'][$node_type->type])) {
        $form['workflow']['node_options']['#options'][$type['type']] = t($type['name']);
      }
    }
  }

  //We are altering the content list form so that we can add out custom options to the filters
  if ($form_id == 'node_admin_content') {
    $session =& $_SESSION['node_overview_filter'];
    $session = is_array($session) ? $session : array();
    $filters = node_filters();
    foreach ($types as $type) {
      $form['filters']['status']['status']['#options'][$type['type'] . '-1'] = t($type['name']);
      $form['filters']['status']['status']['#options'][$type['type'] . '-0'] = t('not ' . $type['name']);
      $form['#submit'][] = 'custom_pub_node_filter_submit';
      $filters['status']['options'][$type['type'] . '-1'] = t($type['name']);
      $filters['status']['options'][$type['type'] . '-0'] = t('not ' . $type['name']);
    }
    $form['filters']['current'] = array();
    foreach ($session as $filter) {
      list($type, $value) = $filter;
      if ($type == 'category') {

        // Load term name from DB rather than search and parse options array.
        $value = module_invoke('taxonomy', 'get_term', $value);
        $value = $value->name;
      }
      elseif ($type == 'language') {
        $value = empty($value) ? t('Language neutral') : module_invoke('locale', 'language_name', $value);
      }
      else {
        $value = $filters[$type]['options'][$value];
      }
      if ($i++) {
        $form['filters']['current'][] = array(
          '#value' => t('<em>and</em> where <strong>%a</strong> is <strong>%b</strong>', array(
            '%a' => $filters[$type]['title'],
            '%b' => $value,
          )),
        );
      }
      else {
        $form['filters']['current'][] = array(
          '#value' => t('<strong>%a</strong> is <strong>%b</strong>', array(
            '%a' => $filters[$type]['title'],
            '%b' => $value,
          )),
        );
      }
      if (in_array($type, array(
        'type',
        'language',
      ))) {

        // Remove the option if it is already being filtered on.
        unset($filters[$type]);
      }
    }
  }
}