Same name and namespace in other branches
  1. 4.6.x developer/hooks/core.php \hook_search_preprocess()
  2. 4.7.x developer/hooks/core.php \hook_search_preprocess()
  3. 5.x developer/hooks/core.php \hook_search_preprocess()
  4. 6.x developer/hooks/core.php \hook_search_preprocess()
  5. 7.x modules/search/search.api.php \hook_search_preprocess()
  6. 8.9.x core/modules/search/search.api.php \hook_search_preprocess()
  7. 9 core/modules/search/search.api.php \hook_search_preprocess()

Preprocess text for search.

This hook is called to preprocess both the text added to the search index and the keywords users have submitted for searching. The same processing needs to be applied to both so that searches will find matches.

Possible uses:

  • Adding spaces between words of Chinese or Japanese text.
  • Stemming words down to their root words to allow matches between, for instance, walk, walked, walking, and walks in searching.
  • Expanding abbreviations and acronyms that occur in text.

Parameters

string $text: The text to preprocess. This is a single piece of plain text extracted from between two HTML tags or from the search query. It will not contain any HTML entities or HTML tags.

string|null $langcode: The language code for the language the text is in, if known. When this hook is invoked during search indexing, the language will most likely be known and passed in. This is left up to the search plugin; \Drupal\node\Plugin\Search\NodeSearch does pass in the node language. However, when this hook is invoked during searching, in order to let a module apply the same preprocessing to the search keywords and indexed text so they will match, $langcode will be NULL. A hook implementation can call the getCurrentLanguage() method on the 'language_manager' service to determine the current language and act accordingly.

Return value

string The text after preprocessing. Note that if your module decides not to alter the text, it should return the original text. Also, after preprocessing, words in the text should be separated by a space.

Related topics

1 function implements hook_search_preprocess()

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

search_langcode_test_search_preprocess in core/modules/search/tests/modules/search_langcode_test/search_langcode_test.module
Implements hook_search_preprocess().

File

core/modules/search/search.api.php, line 49
Hooks provided by the Search module.

Code

function hook_search_preprocess($text, $langcode = NULL) {

  // If the language is not set, get it from the language manager.
  if (!isset($langcode)) {
    $langcode = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
  }

  // If the langcode is set to 'en' then add variations of the word "testing"
  // which can also be found during English language searches.
  if ($langcode == 'en') {

    // Add the alternate verb forms for the word "testing".
    if ($text == 'we are testing') {
      $text .= ' test tested';
    }
  }
  return $text;
}