From 68c4ff4995f690f9c75c01770c9f825efc9b1425 Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Tue, 20 Feb 2024 18:49:08 +0530 Subject: [PATCH] PER-10: Added new search index field for image discovery --- .../IslandoraObjectImageDiscovery.php | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/Plugin/search_api/processor/IslandoraObjectImageDiscovery.php diff --git a/src/Plugin/search_api/processor/IslandoraObjectImageDiscovery.php b/src/Plugin/search_api/processor/IslandoraObjectImageDiscovery.php new file mode 100644 index 0000000..94929a3 --- /dev/null +++ b/src/Plugin/search_api/processor/IslandoraObjectImageDiscovery.php @@ -0,0 +1,117 @@ +imageDiscovery = $image_discovery; + $this->entityTypeManager = $entity_type_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('dgi_image_discovery.service'), + $container->get('entity_type.manager') + ); + } + + /** + * {@inheritdoc} + */ + public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) { + $properties = []; + + if (!$datasource) { + $definition = [ + 'label' => $this->t('Islandora Object Image Discovery'), + 'description' => $this->t('Styled Image Url which can then be passed to the image src.'), + 'type' => 'string', + 'is_list' => FALSE, + 'processor_id' => $this->getPluginId(), + ]; + $properties['islandora_object_image_discovery'] = new ProcessorProperty($definition); + } + + return $properties; + } + + /** + * {@inheritdoc} + */ + public function addFieldValues(ItemInterface $item) { + $entity = $item->getOriginalObject()->getValue(); + $value = NULL; + + // Get the image discovery URL. + if (!$entity->isNew() && $entity instanceof NodeInterface) { + $event = $this->imageDiscovery->getImage($entity); + if ($event->hasMedia()) { + $value = $this->entityTypeManager->getStorage('image_style')->load('solr_grid_thumbnail') + ->buildUrl($event->getMedia()->field_media_image->entity->getFileUri()); + } + } + + $fields = $item->getFields(FALSE); + $fields = $this->getFieldsHelper()->filterForPropertyPath($fields, NULL, 'islandora_object_image_discovery'); + foreach ($fields as $field) { + $field->addValue($value); + } + } + +}