Search interface

Same name in other branches
  1. 7.x modules/search/search.module \search
  2. 9 core/modules/search/search.module \search
  3. 10 core/modules/search/search.module \search
  4. 11.x 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/mymodule) so that users can easily find it.

See also

Plugin API

Annotations

File

core/modules/search/search.module, line 436

Functions

Title Sort descending Deprecated File name Summary
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 Deprecated core/modules/search/search.module Updates the full-text search index for a particular item.

Classes

Title Sort descending File name Summary
SearchPlugin core/modules/search/src/Annotation/SearchPlugin.php Defines a SearchPlugin type annotation object.

Interfaces

Title Sort descending File name Summary
SearchIndexInterface core/modules/search/src/SearchIndexInterface.php Provides search index management functions.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.