You are here

function commons_search_form_alter in Drupal Commons 7.3

Custom search form that switches between Core and Solr depending on which is the enabled search module.

File

modules/commons/commons_search/commons_search.module, line 37

Code

function commons_search_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_block_form') {

    // Move the textfield to after the drop down.
    $site_key = 'c-all';
    $form['search_block_form']['#weight'] = 1;
    if (module_exists('commons_search_core')) {

      // We're using faceted core search, so change Site search key to reflect
      // the change of search engines.
      $site_key = 'o-search_facetapi';
    }
    else {
      if (module_exists('apachesolr_search')) {

        // Are we using Apache Solr? If so change the Site search key to reflect
        // the change of search engines.
        $site_key = 'o-solr';
      }
    }

    // Overwrite all options so we only have 3 (at most).
    $form['custom_search_types']['#options'] = array(
      $site_key => t('Site'),
      'c-user' => t('Users'),
    );
    $group = FALSE;
    $node = current_path();
    if ((strrpos($node, 'node/') !== FALSE || strrpos($node, 'group/') !== FALSE) && is_numeric(substr($node, strrpos($node, '/') + 1))) {

      // If we're on a page that looks like a group node, see if it is actually
      // a group node.
      $node = node_load(substr($node, strrpos($node, '/') + 1));
      if (!empty($node)) {
        $group = $node->type == 'group';
      }
    }
    if ($group) {

      // Current page is a group node, so add group search option to the
      // dropdown menu and set the current group id.
      $form['custom_search_types']['#options']['o-commons_search'] = t('This group');
      $form['custom_search_types']["#default_value"] = 'o-commons_search';
      $form_state['search_group_id'] = $node->nid;
    }
    if (arg(0) == 'search') {

      // If we've already searched, refill the search box with the current
      // search keywords.
      $form['search_block_form']['#default_value'] = check_plain(arg(2));
      if (arg(1) == 'user') {
        $form['custom_search_types']["#default_value"] = 'c-user';
      }
    }
    $keys = arg(2);
    if (!module_exists('commons_search_solr') && !empty($keys)) {

      // Remove the search keys from the form action only if we're using core search.
      $form['#action'] = str_replace("/" . check_plain(arg(2)), '', $form['#action']);
    }
  }
}