Drupal 8: Creating Custom Fields In Search API

25th December 2020 - 10 minutes read time

Pushing data from Drupal into Solr is a really convenient way of creating a robust and extensible search solution. The Search API module has a number of different fields available that can be used to integrate with all sorts of fields, but what isn't included is computed fields or other data.

Thankfully, adding a custom field into the Search API system doesn't need a lot of code. A single class, with an optional hook, is all that's needed to het everything working.

I was recently looking at the node view count module that was being used to record what users viewed what nodes on a Drupal site. What was needed was a report page that had a bunch of data from different fields of a node, along with the node view count data. As this data wasn't immediately available to Solr I needed to find a way to inject the data into Solr using the mechanisms that Search API has. 

To create a Search API processor plugin you need to create a class in the directory src/Plugin/search_api/processor (inside your custom module) that extends the ProcessorPluginBase class from the Search API module. The plugin definition is in the form of an annotation that appears at the top of the class.

  1. <?php
  2.  
  3. namespace Drupal\my_module\Plugin\search_api\processor;
  4.  
  5. use Drupal\search_api\Processor\ProcessorPluginBase;
  6.  
  7. /**
  8.  * Adds the item's view count to the indexed data.
  9.  *
  10.  * @SearchApiProcessor(
  11.  * id = "add_view_count",
  12.  * label = @Translation("View Count"),
  13.  * description = @Translation("Adds the items view count."),
  14.  * stages = {
  15.  * "add_properties" = 0,
  16.  * },
  17.  * locked = true,
  18.  * hidden = true,
  19.  * )
  20.  */
  21. class AddViewCount extends ProcessorPluginBase {
  22. }

There are a couple of methods that we need to create in order to get this working.

The first method needed tells the Search API module what properties are available. This needs to be called getPropertyDefinition() and should return an array of properties that this plugin defines. For the purposes of this situation we just need to ensure that a data source exists and define a single field called "View Count".

  1. /**
  2.  * {@inheritdoc}
  3.  */
  4. public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
  5. $properties = [];
  6.  
  7. if (!$datasource) {
  8. $definition = [
  9. 'label' => $this->t('View Count'),
  10. 'description' => $this->t('The view count for the item'),
  11. 'type' => 'integer',
  12. 'processor_id' => $this->getPluginId(),
  13. ];
  14. $properties['search_api_view_count'] = new ProcessorProperty($definition);
  15. }
  16.  
  17. return $properties;
  18. }

With the property in place, we can define the processor to inject the data into the field.

Before we can include the data from the node view counts module we first need to inject the service the module defines into our Search API plugin. This just uses the create() method that gets called when the plugin is created and injects the node view count service into the object.

  1. /**
  2.  * Node view count records manager.
  3.  *
  4.  * @var \Drupal\nodeviewcount\NodeViewCountRecordsManager
  5.  */
  6. protected $nodeViewCountRecordsManager;
  7.  
  8. /**
  9.  * {@inheritdoc}
  10.  */
  11. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  12. /** @var static $processor */
  13. $processor = parent::create($container, $configuration, $plugin_id, $plugin_definition);
  14.  
  15. $processor->setNodeViewCountRecordsManager($container->get('nodeviewcount.records_manager'));
  16.  
  17. return $processor;
  18. }
  19.  
  20. /**
  21.  * Sets the nodeviewcount.records_manager service.
  22.  *
  23.  * @param \Drupal\nodeviewcount\NodeViewCountRecordsManager $nodeViewCountRecordsManager
  24.  * The nodeviewcount.records_manager service.
  25.  */
  26. public function setNodeViewCountRecordsManager(NodeViewCountRecordsManager $nodeViewCountRecordsManager) {
  27. $this->nodeViewCountRecordsManager = $nodeViewCountRecordsManager;
  28. }

With all that in place we can now define the method that is used to inject the value of the field into the search system. This method is called addFieldValues() and is passed an $item parameter which is a wrapped entity from Drupal. As the data we are interested in is only attached to nodes we just make sure that we are looking at a node before extracting the node view count for the node and adding that as a value. We then pull out the correct property from the search item (which is out view count field) and then inject our view count data into this field. It is also important to set some form of default here or you'll get blank or null in your results.

  1. /**
  2.  * {@inheritdoc}
  3.  */
  4. public function addFieldValues(ItemInterface $item) {
  5. $datasourceId = $item->getDatasourceId();
  6. if ($datasourceId == 'entity:node') {
  7. // This is a node entity so we need to find out if it has a view count.
  8. $node = $item->getOriginalObject()->getValue();
  9. $nodeViewCount = $this->nodeViewCountRecordsManager->getNodeViewsCount($node);
  10.  
  11. if ($nodeViewCount) {
  12. // A view count was found, add it to the relevant field.
  13. $fields = $this->getFieldsHelper()->filterForPropertyPath($item->getFields(), NULL, 'search_api_view_count');
  14. foreach ($fields as $field) {
  15. if (isset($nodeViewCount[0])) {
  16. $field->addValue($nodeViewCount[0]->expression);
  17. }
  18. else {
  19. $field->addValue(0);
  20. }
  21. }
  22. }
  23. }
  24. }

The plugin is now complete but it won't do anything unless we first plug it into the Solr search interface. To add this field visit the fields page for your index and click "Add fields", you should see the following in the list of fields that can be added.

Drupal Search API add custom field

When you click add in the above dialogue against this field it will be added to the list of set fields in your index.

Drupal Search API added field

The field is now ready to use, but remember that you must run a full re-index on the Sorl index for there to be any data available in it. Once that is done the new field is available to use as you would any other search field in search results or as a search filter.

There is one small thing missing from this setup though, which I hinted as being an optional hook before. The first time we index this field we get all of the correct values, but the values are only updated after this point if the node is re-saved. As a result we need to add a little bit of code to allow the view count to be updated when a node is viewed. To do this we use an implementation of hook_nodeviewcount_insert(), which is a hook defined by the node view count module itself. Using this hook we can react to the newly inserted view information and mark this node as needing to be updated in the search index. This is done by finding the available indexes for the entity we are looking at (ie, the node) and then calling the trackItemsUpdated() method on any available indexes that are found.

  1. <?php
  2.  
  3. use Drupal\node\NodeInterface;
  4. use Drupal\search_api\Plugin\search_api\datasource\ContentEntity;
  5.  
  6. /**
  7. * Implements hook_nodeviewcount_insert().
  8. */
  9. function my_module_nodeviewcount_insert(NodeInterface $node, $view_mode) {
  10. // A new view is about to be recorded so we need to tell solr to re-index this node.
  11. $indexes = ContentEntity::getIndexesForEntity($node);
  12. if (!$indexes) {
  13. return;
  14. }
  15.  
  16. $nodeId = $node->id();
  17.  
  18. $itemIds = [];
  19. foreach ($node->getTranslationLanguages() as $langcode => $language) {
  20. $itemIds[] = $nodeId . ':' . $langcode;
  21. }
  22.  
  23. foreach ($indexes as $index) {
  24. $index->trackItemsUpdated('entity:node', $itemIds);
  25. }
  26. }

Now, when a node is viewed the node count is updated in the database and then updated in the Solr search index. The trackItemsUpdated() method calls methods to update the index at the end of the page request, so in theory it shouldn't cause any slowdown or disruption to users.

I have skipped over some of the fine details in the implementation here, but the solution here will work for any sort of computed field. Also, although I have used this extensively on Drupal 8, I haven't not tried this on Drupal 9 yet due to the node view count module not having a Drupal 9 version. There is nothing about the rest of these implementation details that shouldn't work with other computed fields though. 

For completeness, here is the full code for the Search API plugin, along with all of the needed use statements that weren't included in the above examples.

  1. <?php
  2.  
  3. namespace Drupal\my_module\Plugin\search_api\processor;
  4.  
  5. use Drupal\search_api\Datasource\DatasourceInterface;
  6. use Drupal\search_api\Item\ItemInterface;
  7. use Drupal\search_api\Processor\ProcessorPluginBase;
  8. use Drupal\search_api\Processor\ProcessorProperty;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use Drupal\nodeviewcount\NodeViewCountRecordsManager;
  11.  
  12. /**
  13.  * Adds the item's view count to the indexed data.
  14.  *
  15.  * @SearchApiProcessor(
  16.  * id = "add_view_count",
  17.  * label = @Translation("View Count"),
  18.  * description = @Translation("Adds the items view count."),
  19.  * stages = {
  20.  * "add_properties" = 0,
  21.  * },
  22.  * locked = true,
  23.  * hidden = true,
  24.  * )
  25.  */
  26. class AddViewCount extends ProcessorPluginBase {
  27.  
  28. /**
  29.   * Node view count records manager..
  30.   *
  31.   * @var \Drupal\nodeviewcount\NodeViewCountRecordsManager
  32.   */
  33. protected $nodeViewCountRecordsManager;
  34.  
  35. /**
  36.   * {@inheritdoc}
  37.   */
  38. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  39. /** @var static $processor */
  40. $processor = parent::create($container, $configuration, $plugin_id, $plugin_definition);
  41.  
  42. $processor->setNodeViewCountRecordsManager($container->get('nodeviewcount.records_manager'));
  43.  
  44. return $processor;
  45. }
  46.  
  47. /**
  48.   * Sets the nodeviewcount.records_manager service.
  49.   *
  50.   * @param \Drupal\nodeviewcount\NodeViewCountRecordsManager $nodeViewCountRecordsManager
  51.   * The nodeviewcount.records_manager service.
  52.   */
  53. public function setNodeViewCountRecordsManager(NodeViewCountRecordsManager $nodeViewCountRecordsManager) {
  54. $this->nodeViewCountRecordsManager = $nodeViewCountRecordsManager;
  55. }
  56.  
  57. /**
  58.   * {@inheritdoc}
  59.   */
  60. public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) {
  61. $properties = [];
  62.  
  63. if (!$datasource) {
  64. $definition = [
  65. 'label' => $this->t('View Count'),
  66. 'description' => $this->t('The view count for the item'),
  67. 'type' => 'integer',
  68. 'processor_id' => $this->getPluginId(),
  69. ];
  70. $properties['search_api_view_count'] = new ProcessorProperty($definition);
  71. }
  72.  
  73. return $properties;
  74. }
  75.  
  76. /**
  77.   * {@inheritdoc}
  78.   */
  79. public function addFieldValues(ItemInterface $item) {
  80. $datasourceId = $item->getDatasourceId();
  81. if ($datasourceId == 'entity:node') {
  82. // This is a node entity so we need to find out if it has a view count.
  83. $node = $item->getOriginalObject()->getValue();
  84. $nodeViewCount = $this->nodeViewCountRecordsManager->getNodeViewsCount($node);
  85.  
  86. if ($nodeViewCount) {
  87. // A view count was found, add it to the relevant field.
  88. $fields = $this->getFieldsHelper()->filterForPropertyPath($item->getFields(), NULL, 'search_api_view_count');
  89. foreach ($fields as $field) {
  90. if (isset($nodeViewCount[0])) {
  91. $field->addValue($nodeViewCount[0]->expression);
  92. }
  93. else {
  94. $field->addValue(0);
  95. }
  96. }
  97. }
  98. }
  99. }
  100.  
  101. }

 

Add new comment

The content of this field is kept private and will not be shown publicly.