function search_api_autocomplete_autocomplete in Search API Autocomplete 7
Page callback for getting autocomplete suggestions.
Parameters
SearchApiAutocompleteSearch $search: The search for which to retrieve autocomplete suggestions.
string $fields: A comma-separated list of fields on which to do autocompletion. Or "-" to use all fulltext fields.
string $keys: The user input so far.
1 string reference to 'search_api_autocomplete_autocomplete'
- search_api_autocomplete_menu in ./
search_api_autocomplete.module - Implements hook_menu().
File
- ./
search_api_autocomplete.pages.inc, line 19 - Contains page callbacks and theme functions for the frontend UI.
Code
function search_api_autocomplete_autocomplete(SearchApiAutocompleteSearch $search, $fields, $keys = '') {
$ret = array();
try {
if ($search
->supportsAutocompletion()) {
// Transliterate keys if applicable.
if (_search_api_autocomplete_index_uses_translation($search
->index())) {
$keys = transliteration_get($keys);
}
list($complete, $incomplete) = $search
->splitKeys($keys);
$query = $search
->getQuery($complete, $incomplete);
if ($query) {
// @todo Maybe make range configurable?
$query
->range(0, 10);
$query
->setOption('search id', 'search_api_autocomplete:' . $search->machine_name);
if (!empty($search->options['fields'])) {
$query
->fields($search->options['fields']);
}
elseif ($fields != '-') {
$fields = explode(' ', $fields);
$query
->fields($fields);
}
$suggestions = $search
->getSuggester()
->getAutocompleteSuggestions($query, $incomplete, $keys);
if ($suggestions) {
foreach ($suggestions as $suggestion) {
// Convert suggestion strings into an array.
if (is_string($suggestion)) {
$pos = strpos($suggestion, $keys);
if ($pos === FALSE) {
$suggestion = array(
'user_input' => '',
'suggestion_suffix' => $suggestion,
);
}
else {
$suggestion = array(
'suggestion_prefix' => substr($suggestion, 0, $pos),
'user_input' => $keys,
'suggestion_suffix' => substr($suggestion, $pos + strlen($keys)),
);
}
}
// Add defaults.
$suggestion += array(
'url' => NULL,
'keys' => NULL,
'prefix' => NULL,
'suggestion_prefix' => '',
'user_input' => $keys,
'suggestion_suffix' => '',
'results' => NULL,
);
if (empty($search->options['results'])) {
unset($suggestion['results']);
}
// Decide what the action of the suggestion is – entering specific
// search terms or redirecting to a URL.
if (isset($suggestion['url'])) {
$key = ' ' . $suggestion['url'];
}
else {
// Also set the "keys" key so it will always be available in alter
// hooks and the theme function.
if (!isset($suggestion['keys'])) {
$suggestion['keys'] = $suggestion['suggestion_prefix'] . $suggestion['user_input'] . $suggestion['suggestion_suffix'];
}
$key = trim($suggestion['keys']);
}
if (!isset($ret[$key])) {
$ret[$key] = $suggestion;
}
}
$alter_params = array(
'query' => $query,
'search' => $search,
'incomplete_key' => $incomplete,
'user_input' => $keys,
);
drupal_alter('search_api_autocomplete_suggestions', $ret, $alter_params);
foreach ($ret as $key => $suggestion) {
if (isset($suggestion['render'])) {
$ret[$key] = render($suggestion['render']);
}
else {
$escaped_variables = array(
'keys',
'suggestion_prefix',
'user_input',
'suggestion_suffix',
);
foreach ($escaped_variables as $variable) {
if ($suggestion[$variable]) {
$suggestion[$variable] = check_plain($suggestion[$variable]);
}
}
$ret[$key] = theme('search_api_autocomplete_suggestion', $suggestion);
}
}
}
}
}
} catch (SearchApiException $e) {
watchdog_exception('search_api_autocomplete', $e, '%type while retrieving autocomplete suggestions: !message in %function (line %line of %file).');
}
drupal_json_output($ret);
}