On this page
Providing a new datasource plugin
Last updated on
16 October 2023
This documentation needs work. See "Help improve this page" in the sidebar.
(This page is a stub, merely copy-pasting quick instructions from an answer to a support request issue. If you use it to implement your own datasource, please consider helping others by expanding this to a full documentation page.)
In the Search API, you can add a custom datasource plugin to make new types of data available for searching and indexing.
Example code
- The Search API Solr module adds a datasource for searching Solr contents indexed from outside Drupal (no indexing support necessary in that case, of course).
- The Search API Revisions module implements a datasource that indexes revisions of content entities.
- #3394384: Add support for periodically indexing arbitrary pages (views, contact forms, …) implements a simple datasource that includes support for indexing.
Implementation
Here is also a general list of basic instructions how to go about adding a datasource, with the example of making Views pages and/or blocks searchable:
- Create the datasource as a subclass of
DatasourcePluginBase, implement its missing methods and override all those that just have a placeholder implementation in the base class (likeloadMultiple(),getItemLabel(), etc.), where possible. - For the
ComplexDataInterfaceobjects used throughout that class, you'll probably want your own class wrapping whatever data you have. TheDrupal\Core\TypedData\Plugin\DataType\Mapclass implementsComplexDataInterfaceand may be a useful starting point. You'll also need to subclassComplexDataDefinitionBase. In the example case, views with both their metadata (I guess) and their current result set (or, maybe just result HTML?). - In
getPropertyDefinitions(), in both theDatasourcePluginBasesubclass and theComplexDataDefinitionBasesubclass, you'll have to declare the properties your items can contain. In the example, maybe view name, view description, definitely the ID and then one or more fields for the result set. - Apart from the datasource plugin, you'll also need code that tracks when new items (in your case, Views pages and/or blocks) are added, when they are updated (i.e., should be re-indexed – in your case, probably whenever the result set changes, or maybe just periodically to keep things simple) and when they are deleted – see
search_api_entity_insert()et al. for examples.
Help improve this page
Page status: Needs work
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion