You are here

public function SearchApiAutocompleteServerSuggester::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/SearchApiAutocompleteServerSuggester.php, line 74
Contains SearchApiAutocompleteServerSuggester.

Class

SearchApiAutocompleteServerSuggester
Provides a suggester plugin that retrieves suggestions from the server.

Code

public function getAutocompleteSuggestions(SearchApiQueryInterface $query, $incomplete_key, $user_input) {
  if ($this->configuration['fields']) {
    $query
      ->fields($this->configuration['fields']);
  }
  $query
    ->preExecute();
  return $query
    ->getIndex()
    ->server()
    ->getAutocompleteSuggestions($query, $this
    ->getSearch(), $incomplete_key, $user_input);
}