search.api.php 2.7 KB

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