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

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:

  1. 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 (like loadMultiple(), getItemLabel(), etc.), where possible.
  2. For the ComplexDataInterface objects used throughout that class, you'll probably want your own class wrapping whatever data you have. The Drupal\Core\TypedData\Plugin\DataType\Map class implements ComplexDataInterface and may be a useful starting point.  You'll also need to subclass ComplexDataDefinitionBase. In the example case, views with both their metadata (I guess) and their current result set (or, maybe just result HTML?).
  3. In getPropertyDefinitions(), in both the DatasourcePluginBase subclass and the ComplexDataDefinitionBase subclass, 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.
  4. 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: