Created
May 15, 2018 21:16
-
-
Save simesy/93afcc751047f78bc7ccd212fc8f2016 to your computer and use it in GitHub Desktop.
Search api processor plugin to add entity type as an indexed field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Drupal\PROJECT\Plugin\search_api\processor; | |
| use Drupal\search_api\Datasource\DatasourceInterface; | |
| use Drupal\search_api\Item\ItemInterface; | |
| use Drupal\search_api\Processor\ProcessorPluginBase; | |
| use Drupal\search_api\Processor\ProcessorProperty; | |
| /** | |
| * Adds the item's URL to the indexed data. | |
| * | |
| * @SearchApiProcessor( | |
| * id = "entity_type", | |
| * label = @Translation("Entity type"), | |
| * description = @Translation("Drupal entity type: node, media, file, etc."), | |
| * stages = { | |
| * "add_properties" = 0, | |
| * }, | |
| * locked = true, | |
| * hidden = true, | |
| * ) | |
| */ | |
| class EntityType extends ProcessorPluginBase { | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) { | |
| $properties = []; | |
| if (!$datasource) { | |
| $definition = [ | |
| 'label' => $this->t('Entity type'), | |
| 'description' => $this->t('Drupal entity type: node, media, file, etc.'), | |
| 'type' => 'string', | |
| 'processor_id' => $this->getPluginId(), | |
| ]; | |
| $properties['entity_type'] = new ProcessorProperty($definition); | |
| } | |
| return $properties; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function addFieldValues(ItemInterface $item) { | |
| $entity_type = $item->getOriginalObject()->getValue()->getEntitytype()->id(); | |
| $fields = $this->getFieldsHelper() | |
| ->filterForPropertyPath($item->getFields(), NULL, 'entity_type'); | |
| foreach ($fields as $field) { | |
| $field->addValue($entity_type); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment