You are here

function hook_search_api_autocomplete_suggestions_alter in Search API Autocomplete 8

Same name and namespace in other branches
  1. 7 search_api_autocomplete.api.php \hook_search_api_autocomplete_suggestions_alter()

Alter the suggestions that will be returned for a certain request.

Parameters

\Drupal\search_api_autocomplete\Suggestion\SuggestionInterface[] $suggestions: The suggestions that will be returned.

array $alter_params: An associative array with the following keys:

  • query: The query generated for the request.
  • search: The autocomplete search entity for which suggestions are requested.
  • incomplete_key: The part of the user input considered to be an incomplete word. Might be empty.
  • user_input: The complete user input for the fulltext search keywords.
1 function implements hook_search_api_autocomplete_suggestions_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

search_api_autocomplete_test_hooks_search_api_autocomplete_suggestions_alter in tests/search_api_autocomplete_test_hooks/search_api_autocomplete_test_hooks.search_api_autocomplete.inc
Implements hook_search_api_autocomplete_suggestions_alter().
1 invocation of hook_search_api_autocomplete_suggestions_alter()
AutocompleteController::autocomplete in src/Controller/AutocompleteController.php
Page callback: Retrieves autocomplete suggestions.

File

./search_api_autocomplete.api.php, line 27
Hooks provided by the Search API autocomplete module.

Code

function hook_search_api_autocomplete_suggestions_alter(array &$suggestions, array $alter_params) {

  // Users should really try searching for "mandelbrot" once, so just always
  // suggest that, too. In case the suggestions generated have reached the
  // limit, replace the last suggestion to this end.

  /** @var \Drupal\search_api_autocomplete\SearchInterface $search */
  $search = $alter_params['search'];
  if (count($suggestions) >= $search
    ->getOption('limit')) {
    array_pop($suggestions);
  }
  $suggestions[] = new \Drupal\search_api_autocomplete\Suggestion\Suggestion('mandelbrot');
}