You are here

function suggestion_autocomplete in Autocomplete Search Suggestions 7

AJAX search autocomplete callback.

Parameters

string $txt: The search string.

5 string references to 'suggestion_autocomplete'
suggestion_admin_settings_form_validate in ./suggestion.admin.inc
Custom validation.
suggestion_form_alter in ./suggestion.module
Implements hook_form_alter().
suggestion_menu in ./suggestion.module
Implements hook_menu().
_suggestion_admin_get_autocomplete in ./suggestion.admin.inc
Build an autocomplete form ID to field name hash.
_suggestion_admin_submit_autocomplete in ./suggestion.admin.inc
Build an autocomplete form ID to field name hash.

File

./suggestion.module, line 14
Autocomplete suggestions.

Code

function suggestion_autocomplete($txt = '') {
  $limit = variable_get('suggestion_limit', 20);
  $min = variable_get('suggestion_atoms_min', 3);
  $txt = preg_replace(array(
    '/^[^a-z]+/',
    '/[^a-z]+$/',
  ), array(
    '',
    ' ',
  ), strtolower($txt));
  if (strlen($txt) < variable_get('suggestion_min', 4)) {
    print drupal_json_output(array(
      '' => '',
    ));
    drupal_exit();
  }
  $count = str_word_count($txt);
  $atoms = $count < $min ? $min + 2 : $count + 2;
  $ngram = db_like($txt) . '%';
  $suggestions = SuggestionStorage::getAutocomplete($ngram, $atoms, $limit);
  if (count($suggestions) < $limit) {
    $suggestions += SuggestionStorage::getAutoComplete('%' . $ngram, $atoms, $limit - count($suggestions));
  }
  print count($suggestions) ? drupal_json_output($suggestions) : drupal_json_output(array(
    '' => '',
  ));
  drupal_exit();
}