Snippets

Metadrop SEARCH API - Add custom field using Processors

Created by Agustin Gomez Iglesias last modified
<?php

/**
 * @file
 * Contains \Drupal\MY_MODULE\Plugin\search_api\processor\AddCustomField.
 *
 * A proccesor example that adds some custom fields.
 * 
 * @NOTE 
 *   - Replace any occurrences of MY_MODULE with your module machine_name.
 *   - Name this file AddCustomField.php and place it in the correct folder.
 *
 * @TODO Make some tests.
 */

namespace Drupal\MY_MODULE\Plugin\search_api\processor;

use Drupal\Core\TypedData\DataDefinition;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Processor\ProcessorPluginBase;


/**
 * @SearchApiProcessor(
 *   id = "MY_MODULE_add_custom_field",
 *   label = @Translation("Custom field examples"),
 *   description = @Translation("Adds custom field examples."),
 *   stages = {
 *     "pre_index_save" = -10,
 *     "preprocess_index" = -30
 *   }
 * )
 */
class AddCustomField extends ProcessorPluginBase {
  /**
   * {@inheritdoc}
   *
   */
  public function alterPropertyDefinitions(array &$properties, DatasourceInterface $datasource = NULL) {
    if ($datasource) {
      return;
    }

    // Ensure that our fields are defined.
    $fields = $this->getFieldsDefinition();

    foreach ($fields as $field_id => $field_definition) {
      $properties[$field_id] = new DataDefinition($field_definition);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function preprocessIndexItems(array &$items) {

    /** @var \Drupal\search_api\Item\ItemInterface $item */
    foreach ($items as $item) {

      // First custom field will only be added if we are indexing "nid" field.
      foreach ($this->filterForPropertyPath($item->getFields(), 'nid') as $field_reference) {
        foreach ($this->filterForPropertyPath($item->getFields(), 'MY_MODULE_field_based_on_nid') as $nid_based_field) {
          $nid_based_field->addValue('Node Id: ' . reset($field_reference->getValues()));
        }
      }

      // We will add always this custom field, with random value.
      foreach ($this->filterForPropertyPath($item->getFields(), 'MY_MODULE_field_active_standby') as $always_added_field) {
        $active_standby = rand(0,1) ? 'active' : 'standby';

        $always_added_field->addValue($active_standby);
      }

      // We will add a time() and see how it is indexed.
      foreach ($this->filterForPropertyPath($item->getFields(), 'MY_MODULE_field_date_experiment') as $date_experiment_field) {
        $date_experiment_field->addValue(time());
      }
    }
  }

  /**
   * {@inheritdoc}
   *
   */
  public function preIndexSave() {
    foreach ($this->getFieldsDefinition() as $field_id => $field_definition) {
      // @NOTE
      // If we add the type as third parameter, the user will not be able
      // to change it through the UI (the type will be blocked).
      $this->ensureField(NULL, $field_id, $field_definition['type']);
    }
  }

  /**
   * Helper function for defining our custom fields.
   */
  protected function getFieldsDefinition() {
    $fields['MY_MODULE_field_based_on_nid'] = array(
      'label' => 'Custom field based on nid',
      'description' => 'I will be used to make a text type field with an id if "nid" field exists.',
      'type' => 'text',
      'prefix' => 't', // For Solr fields I think it is ok.
    );
    $fields['MY_MODULE_field_active_standby'] = array(
      'label' => 'Custom field always added, could be used for facets',
      'description' => 'Custom field 2 of type string will be always added with values active/standby.',
      'type' => 'string',
      'prefix' => 's', // For Solr fields I think it is ok.
    );
    $fields['MY_MODULE_field_date_experiment'] = array(
      'label' => 'Custom field of date type, always added',
      'description' => 'I will be added as a time(), but it will be indexed differently.',
      'type' => 'date',
      'prefix' => 'd', // For Solr fields I think it is ok.
    );

    return $fields;
  }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.