You are here

function apachesolr_autocomplete_callback in Apache Solr Autocomplete 6

Same name and namespace in other branches
  1. 7.2 apachesolr_autocomplete.module \apachesolr_autocomplete_callback()
  2. 7 apachesolr_autocomplete.module \apachesolr_autocomplete_callback()

Callback for url apachesolr_autocomplete/autocomplete.

Parameters

$keys: The user-entered query.

1 string reference to 'apachesolr_autocomplete_callback'
apachesolr_autocomplete_menu in ./apachesolr_autocomplete.module
Implementation of hook_menu().

File

./apachesolr_autocomplete.module, line 98
Alters search forms to suggest terms using Apache Solr using AJAX. Thanks to robertDouglass who contributed some of the code.

Code

function apachesolr_autocomplete_callback($keys = '') {
  if (apachesolr_autocomplete_variable_get_widget() == 'custom') {

    // Keys for custom widget come from $_GET.
    $keys = $_GET['query'];
  }
  $suggestions = array();
  $suggestions = array_merge($suggestions, apachesolr_autocomplete_suggest_word_completion($keys, 5));
  if (apachesolr_autocomplete_variable_get_suggest_keywords() || apachesolr_autocomplete_variable_get_suggest_spellcheck()) {
    $suggestions = array_merge($suggestions, apachesolr_autocomplete_suggest_additional_term($keys, 5));
  }
  $result = array();
  $show_counts = apachesolr_autocomplete_variable_get_counts();
  if (apachesolr_autocomplete_variable_get_widget() == 'custom') {

    // Place suggestions into new array for returning as JSON.
    foreach ($suggestions as $key => $suggestion) {
      $display = theme($suggestion['theme'], $suggestion, $show_counts);
      $result[] = array(
        "key" => substr($key, 1),
        "display" => $display,
      );
    }
  }
  else {
    foreach ($suggestions as $key => $suggestion) {
      $display = theme($suggestion['theme'], $suggestion, $show_counts);
      $result[substr($key, 1)] = $display;
    }
  }
  drupal_json($result);
  exit;
}