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/mymodule) so that users can easily find it.
See also
Plugin API
File
- core/modules/search/search.module, line 592
- Enables site-wide keyword searching.
Functions
Name | 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. |
search_index | core/modules/search/search.module | Updates the full-text search index for a particular item. |
Classes
Name | Location | Description |
---|---|---|
SearchPlugin | core/modules/search/src/Annotation/SearchPlugin.php | Defines a SearchPlugin type annotation object. |
Please login to continue.