diff --git a/src/Plugin/search_api/processor/EmbargoProcessor.php b/src/Plugin/search_api/processor/EmbargoProcessor.php new file mode 100644 index 0000000..ad07ce2 --- /dev/null +++ b/src/Plugin/search_api/processor/EmbargoProcessor.php @@ -0,0 +1,133 @@ +entityTypeManager = $entity_type_manager; + $this->storage = $entity_type_manager->getStorage('embargo'); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity_type.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) { + $properties = []; + + if ($datasource) { + $definition = [ + 'label' => $this->t('Embargo Status'), + 'description' => $this->t('Field to pass embargo status to solr index.'), + 'type' => 'string', + 'processor_id' => $this->getPluginId(), + 'is_list' => TRUE, + ]; + $properties['embargo_status'] = new CustomValueProperty($definition); + } + + return $properties; + } + + /** + * {@inheritdoc} + */ + public function addFieldValues(ItemInterface $item) { + $embargo_expiration_type = $embargo_type = []; + + // Get node ID. + $item_id = $item->getId(); + $nodeId = preg_replace('/^entity:node\/(\d+):en$/', '$1', $item_id); + + // Load node based on ID, and get embargo. + $node = $this->entityTypeManager->getStorage('node')->load($nodeId); + $storages = $this->storage->getApplicableEmbargoes($node); + + if (empty($storages)) { + return; + } + + // Get Embargo details and prepare to pass it to index field. + foreach ($storages as $embargo) { + $allowed_expiration_types = EmbargoInterface::EXPIRATION_TYPES; + $allowed_embargo_type = EmbargoInterface::EMBARGO_TYPES; + // Get Embargo Type. + $embargo_type[] = 'embargo-type-' . $allowed_embargo_type[$embargo->getEmbargoType()]; + + // Get Expiration Type. + $embargo_expiration_type[] = 'embargo-expiration-type-' . $allowed_expiration_types[$embargo->getExpirationType()]; + } + + $embargo_status = $combinedString = implode(' ', $embargo_type) . ' ' . implode(' ', $embargo_expiration_type); + + $fields = $this->getFieldsHelper() + ->filterForPropertyPath($item->getFields(), $item->getDatasourceId(), 'embargo_status'); + foreach ($fields as $field) { + $field->addValue($embargo_status); + } + } + +}