W3cubDocs

/Drupal 8

function hook_search_preprocess

hook_search_preprocess($text, $langcode = NULL)

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

Hooks
Define functions that alter the behavior of Drupal core.
Search interface
The Drupal search interface manages a global search mechanism.

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;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!search!search.api.php/function/hook_search_preprocess/8.1.x