public function SearchApiAutocompleteLiveResultsSuggester::getAutocompleteSuggestions in Search API Autocomplete 7
Retrieves autocompletion suggestions for some user input.
For example, when given the user input "teach us", with "us" being considered incomplete, the following might be returned:
array(
array(
'prefix' => t('Did you mean:'),
'user_input' => 'reach us',
),
array(
'user_input' => 'teach us',
'suggestion_suffix' => 'ers',
),
array(
'user_input' => 'teach us',
'suggestion_suffix' => ' swimming',
),
'teach users swimming',
);
Parameters
SearchApiQueryInterface $query: A query representing the completed user input so far.
string $incomplete_key: The start of another fulltext keyword for the search, which should be completed. Might be empty, in which case all user input up to now was considered completed. Then, additional keywords for the search could be suggested.
string $user_input: The complete user input for the fulltext search keywords so far.
Return value
array An array of suggestions. Each suggestion is either a simple string containing the whole suggested keywords, or an array containing the following keys:
- keys: The keyword (or keywords) this suggestion will autocomplete to. If it is not present, a direct concatenation (no spaces in between) of "suggestion_prefix", "user_input" and "suggestion_suffix" will be used instead.
- url: A URL to which the suggestion should redirect instead of completing the user input in the text field. This overrides the normal behavior and thus makes "keys" obsolete.
- prefix: For special suggestions, some kind of HTML prefix describing them.
- suggestion_prefix: A suggested prefix for the entered input.
- user_input: The input entered by the user. Defaults to $user_input.
- suggestion_suffix: A suggested suffix for the entered input.
- results: If available, the estimated number of results for these keys.
- render: If given, an HTML string or render array which should be displayed to the user for this suggestion. If missing, the suggestion is instead passed to theme_search_api_autocomplete_suggestion().
All the keys are optional, with the exception that at least one of "keys", "url", "suggestion_prefix", "user_input" or "suggestion_suffix" has to be present.
Overrides SearchApiAutocompleteSuggesterInterface::getAutocompleteSuggestions
File
- src/
SearchApiAutocompleteLiveResultsSuggester.php, line 77 - Contains SearchApiAutocompleteLiveResultsSuggester.
Class
- SearchApiAutocompleteLiveResultsSuggester
- Provides a suggester plugin that retrieves Live Results.
Code
public function getAutocompleteSuggestions(SearchApiQueryInterface $query, $incomplete_key, $user_input) {
try {
if ($this->configuration['fields']) {
$query
->fields($this->configuration['fields']);
}
$query
->keys($user_input);
$results = $query
->execute();
} catch (SearchApiException $e) {
return array();
}
$ret = array();
$ids = array();
foreach ((array) $results['results'] as $result) {
$ids[] = $result['id'];
}
$render = NULL;
if (!empty($ids)) {
// Load all searched suggested entities.
$index = $this
->getSearch()
->index();
try {
$datasource = $index
->datasource();
} catch (SearchApiException $e) {
return $ret;
}
$items = $index
->loadItems($ids);
$entity_view = NULL;
$entity_type = $index
->getEntityType();
if ($entity_type && $this->configuration['display'] == 'view_mode') {
$entity_view = entity_view($entity_type, $items, 'live_results_search');
}
foreach ($items as $id => $item) {
$url = NULL;
try {
if ($entity_view) {
$render = drupal_render($entity_view[$entity_type][$id]);
}
else {
$render = $datasource
->getItemLabel($item);
}
$url = $datasource
->getItemUrl($item);
} catch (Exception $e) {
}
if (!$url) {
continue;
}
$ret[] = array(
'url' => url($url['path'], $url['options']),
'render' => $render,
);
}
}
return $ret;
}