You are here

function search_config_form_alter in Search configuration 6

Same name and namespace in other branches
  1. 5 search_config.module \search_config_form_alter()

Implementation of hook_form_alter()

File

./search_config.module, line 24
Used to configure the advanced search form and other search behaviour.

Code

function search_config_form_alter(&$form, &$form_state, $form_id) {

  // Add validation function to search administration form
  if ($form_id == 'search_admin_settings') {
    $form['#validate'][] = 'search_config_validate';
  }

  // Present the user with the appropriate search results tab as defined by
  // variable "search_config_default_search" (labeled "Default Search")
  // in the "Advanced search settings" fieldset in admin/settings/search
  if ($form_id == 'search_theme_form') {
    $form['#submit'] = array(
      'search_config_form_submit',
    );
    $form['module']['#value'] = variable_get('search_config_default_search', 'node');
    $form['module']['#type'] = 'value';
    if (isset($form['search_theme_form_keys'])) {
      $form['processed_keys'] = $form['search_theme_form_keys'];
      $form['processed_keys']['#weight'] = -1;
      unset($form['search_theme_form_keys']);
    }
  }
  if ($form_id == 'search_form' && user_access('use advanced search')) {
    $default_search = variable_get('search_config_default_search', 'node');

    // Keywords
    if (user_access('use keyword search')) {
      if (variable_get('search_config_disable_or', 0)) {
        unset($form['advanced']['keywords']['or']);
      }
      if (variable_get('search_config_disable_phrase', 0)) {
        unset($form['advanced']['keywords']['phrase']);
      }
      if (variable_get('search_config_disable_negative', 0)) {
        unset($form['advanced']['keywords']['negative']);
      }
    }
    else {
      unset($form['advanced']['keywords']);
    }

    // Node types
    // We will need to rebuild the checkboxes for the node types
    $remove = variable_get('search_config_disable_type', array());
    if ($remove['all'] || !user_access('search by node type')) {
      unset($form['advanced']['type']);
    }
    else {
      $types = node_get_types('names');
      foreach ($types as $module => $type) {
        if ($remove[$module]) {
          unset($types[$module]);
        }
        else {
          $types[$module] = t($type);
        }
      }

      // Rebuild form item -- copied from node.module, but only if advanced
      // fieldset exists.
      if (!empty($form['advanced'])) {
        if (count($types) == 1) {
          $type_keys = array_keys($types);
          $form['advanced']['type'] = array(
            '#type' => 'hidden',
            '#default_value' => $type_keys[0],
          );
        }
        else {
          $form['advanced']['type'] = array(
            '#type' => 'checkboxes',
            '#title' => t('Only of the type(s)'),
            '#prefix' => '<div class="criterion">',
            '#suffix' => '</div>',
            '#options' => $types,
          );
        }
      }
    }

    // Taxonomy
    if (variable_get('search_config_disable_category_all', 0) || !user_access('search by category')) {
      if (isset($form['advanced']['category'])) {
        unset($form['advanced']['category']);
      }
    }
    elseif (module_exists('taxonomy')) {
      if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {
        $terms = variable_get('search_config_disable_category', array());
        if (module_exists('og_vocab')) {
          $groupnode = og_get_group_context();
          $sql = "SELECT t.tid FROM ({vocabulary} v, {term_data} t) LEFT JOIN {og_vocab} ov ON v.vid = ov.vid WHERE (v.module = 'og_vocab' AND ov.nid != %d) AND t.vid=v.vid";
          $result = db_query($sql, $groupnode->nid);
          while ($row = db_fetch_object($result)) {
            $terms[$row->tid] = $row->tid;
          }
        }

        // FIXME: What about multiple hierarchy categories?
        foreach ($taxonomy as $vocab => $term) {
          foreach ($term as $k => $v) {
            if (in_array($k, $terms)) {
              unset($taxonomy[$vocab][$k]);
            }
          }

          // Remove empty vocabs
          if (count($taxonomy[$vocab]) == 0) {
            unset($taxonomy[$vocab]);
          }
        }
        if (count($taxonomy) == 0) {
          unset($form['advanced']['category']);
        }
        else {

          // Taxonomy box:
          $form['advanced']['category'] = array(
            '#type' => 'select',
            '#title' => t('Only in the category(s)'),
            '#prefix' => '<div class="criterion">',
            '#size' => 10,
            '#suffix' => '</div>',
            '#options' => $taxonomy,
            '#multiple' => TRUE,
          );
        }
      }
    }
  }
}