You are here

function apachesolr_form_alter in Apache Solr Search 5

Same name and namespace in other branches
  1. 5.2 apachesolr.module \apachesolr_form_alter()

Implementation of hook_form_alter().

File

./apachesolr.module, line 620
Integration with the Apache Solr search application.

Code

function apachesolr_form_alter($form_id, &$form) {
  $arg = arg(1);
  $alias = drupal_lookup_path('alias', "search/{$arg}");

  // Ok, this really sucks. I want a way to know whether the action of the form
  // is supposed to be handled by an ApacheSolr module or not. I manually
  // exclude node and user here, but there are many other hook_search implementations
  // in the wild and this code will potentially interfere with them. It also
  // creates a Solr instance wasting resources.
  if ($alias && $arg != 'node' && $arg != 'user' && (preg_match("&/search/{$arg}&", $form['#action']) || preg_match("&/{$alias}&", $form['#action']) || strpos($form['#action'], $alias))) {
    if (!isset($_POST['form_id'])) {

      // Set up our validation function
      $form['#validate']['apachesolr_search_validate'] = array();

      // if no keys, there's nothing to do.
      if (empty($form['basic']['inline']['keys']['#default_value'])) {
        return;
      }

      // The $query is the true source for search key information
      if ($query =& apachesolr_drupal_query()) {
        $form['basic']['inline']['keys']['#default_value'] = $query
          ->get_query_basic();
      }
    }
  }
}