search.api.php 2.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

/**
 * @file
 * Hooks provided by the Search module.
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
14
 * Preprocess text for search.
15
 *
16
 * This hook is called to preprocess both the text added to the search index
17 18
 * and the keywords users have submitted for searching. The same processing
 * needs to be applied to both so that searches will find matches.
19
 *
20 21 22 23
 * 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.
24
 * - Expanding abbreviations and acronyms that occur in text.
25
 *
26
 * @param string $text
27 28 29
 *   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.
30 31 32 33 34 35 36 37 38 39 40
 * @param 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.
41
 *
42
 * @return string
43 44 45
 *   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.
46 47
 *
 * @ingroup search
48
 */
49
function hook_search_preprocess($text, $langcode = NULL) {
50 51 52 53 54
  // If the language is not set, get it from the language manager.
  if (!isset($langcode)) {
    $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
  }

55 56
  // If the langcode is set to 'en' then add variations of the word "testing"
  // which can also be found during English language searches.
57
  if ($langcode == 'en') {
58 59 60 61 62 63
    // Add the alternate verb forms for the word "testing".
    if ($text == 'we are testing') {
      $text .= ' test tested';
    }
  }

64 65
  return $text;
}
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

/**
 * Alter search plugin definitions.
 *
 * @param array $definitions
 *   The array of search plugin definitions, keyed by plugin ID.
 *
 * @see \Drupal\search\Annotation\SearchPlugin
 * @see \Drupal\search\SearchPluginManager
 */
function hook_search_plugin_alter(array &$definitions) {
  if (isset($definitions['node_search'])) {
    $definitions['node_search']['title'] = t('Nodes');
  }
}

82 83 84 85
/**
 * @} End of "addtogroup hooks".
 */