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