2008-11-25 02:37:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Hooks provided by the Search module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @addtogroup hooks
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-02-27 10:54:12 +00:00
|
|
|
* Preprocess text for search.
|
2008-11-25 02:37:33 +00:00
|
|
|
*
|
2013-02-04 19:11:11 +00:00
|
|
|
* This hook is called to preprocess both the text added to the search index
|
2015-03-31 14:42:56 +00:00
|
|
|
* and the keywords users have submitted for searching. The same processing
|
|
|
|
* needs to be applied to both so that searches will find matches.
|
2008-11-25 02:37:33 +00:00
|
|
|
*
|
2010-02-27 10:54:12 +00:00
|
|
|
* 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.
|
2015-04-02 10:59:11 +00:00
|
|
|
* - Expanding abbreviations and acronyms that occur in text.
|
2008-11-25 02:37:33 +00:00
|
|
|
*
|
2015-03-31 14:42:56 +00:00
|
|
|
* @param string $text
|
2010-02-27 10:54:12 +00:00
|
|
|
* 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.
|
2015-03-31 14:42:56 +00:00
|
|
|
* @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.
|
2012-08-26 18:05:58 +00:00
|
|
|
*
|
2015-03-31 14:42:56 +00:00
|
|
|
* @return string
|
2013-02-04 19:11:11 +00:00
|
|
|
* 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.
|
2009-08-29 21:05:16 +00:00
|
|
|
*
|
|
|
|
* @ingroup search
|
2008-11-25 02:37:33 +00:00
|
|
|
*/
|
2012-08-26 18:05:58 +00:00
|
|
|
function hook_search_preprocess($text, $langcode = NULL) {
|
2015-03-31 14:42:56 +00:00
|
|
|
// If the language is not set, get it from the language manager.
|
|
|
|
if (!isset($langcode)) {
|
|
|
|
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
|
|
|
|
}
|
|
|
|
|
2012-08-26 18:05:58 +00:00
|
|
|
// If the langcode is set to 'en' then add variations of the word "testing"
|
|
|
|
// which can also be found during English language searches.
|
2015-03-31 14:42:56 +00:00
|
|
|
if ($langcode == 'en') {
|
2012-08-26 18:05:58 +00:00
|
|
|
// Add the alternate verb forms for the word "testing".
|
|
|
|
if ($text == 'we are testing') {
|
|
|
|
$text .= ' test tested';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-25 02:37:33 +00:00
|
|
|
return $text;
|
|
|
|
}
|
2014-06-30 13:28:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-07 20:03:26 +00:00
|
|
|
/**
|
|
|
|
* @} End of "addtogroup hooks".
|
|
|
|
*/
|