Security update to Drupal 8.4.6
[yaffs-website] / web / core / modules / search / search.api.php
1 <?php
2
3 /**
4  * @file
5  * Hooks provided by the Search module.
6  */
7
8 /**
9  * @addtogroup hooks
10  * @{
11  */
12
13 /**
14  * Preprocess text for search.
15  *
16  * This hook is called to preprocess both the text added to the search index
17  * and the keywords users have submitted for searching. The same processing
18  * needs to be applied to both so that searches will find matches.
19  *
20  * Possible uses:
21  * - Adding spaces between words of Chinese or Japanese text.
22  * - Stemming words down to their root words to allow matches between, for
23  *   instance, walk, walked, walking, and walks in searching.
24  * - Expanding abbreviations and acronyms that occur in text.
25  *
26  * @param string $text
27  *   The text to preprocess. This is a single piece of plain text extracted
28  *   from between two HTML tags or from the search query. It will not contain
29  *   any HTML entities or HTML tags.
30  * @param string|null $langcode
31  *   The language code for the language the text is in, if known. When this hook
32  *   is invoked during search indexing, the language will most likely be known
33  *   and passed in. This is left up to the search plugin;
34  *   \Drupal\node\Plugin\Search\NodeSearch does pass in the node
35  *   language. However, when this hook is invoked during searching, in order to
36  *   let a module apply the same preprocessing to the search keywords and
37  *   indexed text so they will match, $langcode will be NULL. A hook
38  *   implementation can call the getCurrentLanguage() method on the
39  *   'language_manager' service to determine the current language and act
40  *   accordingly.
41  *
42  * @return string
43  *   The text after preprocessing. Note that if your module decides not to
44  *   alter the text, it should return the original text. Also, after
45  *   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   // If the language is not set, get it from the language manager.
51   if (!isset($langcode)) {
52     $langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
53   }
54
55   // If the langcode is set to 'en' then add variations of the word "testing"
56   // which can also be found during English language searches.
57   if ($langcode == 'en') {
58     // Add the alternate verb forms for the word "testing".
59     if ($text == 'we are testing') {
60       $text .= ' test tested';
61     }
62   }
63
64   return $text;
65 }
66
67 /**
68  * Alter search plugin definitions.
69  *
70  * @param array $definitions
71  *   The array of search plugin definitions, keyed by plugin ID.
72  *
73  * @see \Drupal\search\Annotation\SearchPlugin
74  * @see \Drupal\search\SearchPluginManager
75  */
76 function hook_search_plugin_alter(array &$definitions) {
77   if (isset($definitions['node_search'])) {
78     $definitions['node_search']['title'] = t('Nodes');
79   }
80 }
81
82 /**
83  * @} End of "addtogroup hooks".
84  */