drupaldrupal-8drupal-search

Drupal's Search API with Paragraphs and Reverse references


I would like to configure Drupal's Search API in that way, that it's possible to index (and search over) an entity type (Person) which is referenced by an paragraph and this paragraph is referenced by another entity type (Article).

That's approx. what I have. Article and Person are content types. Editing an article let's you choose a person in a specific role, which is saved as paragraph. That all work's great so far!

                         +-----------+
                         | Paragraph |
                         +-----------+
                  +------> ID: 1     |           +---------+
+---------+       |      | P_ID: 200 <-----+     | Person  |
| Article |       |      | ...       |     |     +---------+
+---------+       |      +-----------+     +-----+ ID: 200 |
| ID:100  |       |                              | ...     |
| P_ID: 1 +-------+                              +---------+
| ...     |
+---------+

+--------+------------------+
| Person | Title of Article |
+---------------------------+
| John   | T1, T3, T6       |
| Alice  | T2               |
| Bob    | T4, T5           |
+--------+------------------+

What I like to have is a Search-API search index with person name and the linked articles as a comma-separated list. (There are persons without articles.)

Is that possible to do that with Drupal's Search API? And if yes, how? On which content type do I start? Do I need to use the Reverse reference fields?


Solution

  • The Search API module allows other modules (custom one in your case) to provide additional properties that can be indexed by implementing a processor plugin. These properties will then be listed just like the "normal" ones on the search index's "Add fields" form.

    The simplest example for such a processor is the "URL field" processor, which adds the "URI" field (which can be found in the "General" datasource section). You can view the latest version of that processor's code here.

    I will not explain the whole coding solution here (it's quite long). There is an excellente documentation on Drupal 8 Search API for this purpose: Create custom fields using a custom processor

    Here an another usefull link: Make adding new properties/fields more a first-class operation of processors


    Here a quick summup what you will need:

    1. Create a custom module
    2. Create a new search_api's Processos Plugin on this module

      Example with a file on my_module under my_module/src/Plugin/search_api/processor/AddPersonName.php

      <?php
      
       namespace Drupal\my_module\Plugin\search_api\processor;
      
       /**
       * Custom Search API processos.
       *
       * @SearchApiProcessor(
       *   ...
       *   ...
       *   ...
       * )
       */
       class AddPersonName extends ProcessorPluginBase {
         // ...
       }
      
    3. Define custom properties on the AddPersonName::getPropertyDefinitions()

    4. Computing property values at indexing time on the AddPersonName::addFieldValues()
    5. Configure your new Plugin in your Search API Index (so in your Drupal administration)

    I hope this tutorial will help you.