You are here

public function Server::getAutocompleteSuggestions in Search API Autocomplete 8

Retrieves autocompletion suggestions for some user input.

For example, when given the user input "teach us", with "us" being considered incomplete, \Drupal\search_api_autocomplete\SuggestionInterface objects representing the following suggestions might be returned:

[
  [
    'prefix' => t('Did you mean:'),
    'user_input' => 'reach us',
  ],
  [
    'user_input' => 'teach us',
    'suggestion_suffix' => 'ers',
  ],
  [
    'user_input' => 'teach us',
    'suggestion_suffix' => ' swimming',
  ],
];

Parameters

\Drupal\search_api\Query\QueryInterface $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

\Drupal\search_api_autocomplete\Suggestion\SuggestionInterface[] An array of autocomplete suggestions.

Overrides SuggesterInterface::getAutocompleteSuggestions

File

src/Plugin/search_api_autocomplete/suggester/Server.php, line 160

Class

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

Namespace

Drupal\search_api_autocomplete\Plugin\search_api_autocomplete\suggester

Code

public function getAutocompleteSuggestions(QueryInterface $query, $incomplete_key, $user_input) {
  $index = $query
    ->getIndex();
  if (!($backend = static::getBackend($index))) {
    return [];
  }

  // If the "Transliteration" processor is enabled for the search index, we
  // also need to transliterate the user input for autocompletion.
  if ($index
    ->isValidProcessor('transliteration')) {
    $langcode = $this
      ->getLanguageManager()
      ->getCurrentLanguage()
      ->getId();
    $incomplete_key = $this
      ->getTransliterator()
      ->transliterate($incomplete_key, $langcode);
    $user_input = $this
      ->getTransliterator()
      ->transliterate($user_input, $langcode);
  }
  if ($this->configuration['fields']) {
    $query
      ->setFulltextFields($this->configuration['fields']);
  }
  $query
    ->preExecute();
  return $backend
    ->getAutocompleteSuggestions($query, $this
    ->getSearch(), $incomplete_key, $user_input);
}