Same name and namespace in other branches
  1. 4.6.x modules/search.module \search
  2. 4.7.x modules/search.module \search
  3. 5.x modules/search/search.module \search
  4. 6.x modules/search/search.module \search
  5. 7.x modules/search/search.module \search
  6. 8.9.x core/modules/search/search.module \search
  7. 9 core/modules/search/search.module \search

The Drupal search interface manages a global search mechanism.

Modules may plug into this system to provide searches of different types of data. Most of the system is handled by the Search module, so this must be enabled for all of the search features to work.

There are two ways to interact with the search system:

  • Specifically for searching nodes, you can implement hook_node_update_index() and hook_node_search_result(). However, note that the search system already indexes all visible output of a node; i.e., everything displayed normally during node viewing. This is usually sufficient. You should only use this mechanism if you want additional, non-visible data to be indexed.
  • Define a plugin implementing \Drupal\search\Plugin\SearchInterface and annotated as \Drupal\search\Annotation\SearchPlugin. This will create a search page type that users can use to set up one or more search pages. Each of these corresponds to a tab on the /search page, which can be used to perform searches. You will also need to implement the execute() method from the interface to perform the search. A base class is provided in \Drupal\search\Plugin\SearchPluginBase. For more information about plugins, see the Plugin API topic.

If your module needs to provide a more complicated search form, then you need to implement it yourself. In that case, you may wish to define it as a local task (tab) under the /search page (e.g. /search/my_module) so that users can easily find it.

See also

Plugin API

Annotations

File

core/modules/search/search.module, line 84
Enables site-wide keyword searching.

Functions

Namesort descending Location Description
hook_search_preprocess core/modules/search/search.api.php Preprocess text for search.
search_excerpt core/modules/search/search.module Returns snippets from a piece of text, with search keywords highlighted.

Classes

Namesort descending Location Description
SearchPlugin core/modules/search/src/Annotation/SearchPlugin.php Defines a SearchPlugin type annotation object.

Interfaces

Namesort descending Location Description
SearchIndexInterface core/modules/search/src/SearchIndexInterface.php Provides search index management functions.