From 68c4ff4995f690f9c75c01770c9f825efc9b1425 Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Tue, 20 Feb 2024 18:49:08 +0530 Subject: [PATCH 1/5] 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); + } + } + +} From 227fa2d72cd75c79e340d6b846c504e52f6686e0 Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Tue, 20 Feb 2024 19:08:40 +0530 Subject: [PATCH 2/5] PER-10: Fixes to errors reported --- .../search_api/processor/IslandoraObjectImageDiscovery.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Plugin/search_api/processor/IslandoraObjectImageDiscovery.php b/src/Plugin/search_api/processor/IslandoraObjectImageDiscovery.php index 94929a3..9669be9 100644 --- a/src/Plugin/search_api/processor/IslandoraObjectImageDiscovery.php +++ b/src/Plugin/search_api/processor/IslandoraObjectImageDiscovery.php @@ -102,8 +102,11 @@ public function addFieldValues(ItemInterface $item) { 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()); + $image = $event->getMedia()->field_media_image; + if (!empty($image)) { + $value = $this->entityTypeManager->getStorage('image_style')->load('solr_grid_thumbnail') + ->buildUrl($image->entity->getFileUri()); + } } } From 531e01b06bbd0345509f0dd80504d279502b41c5 Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Thu, 22 Feb 2024 18:44:57 +0530 Subject: [PATCH 3/5] PER-10: Addressed PR feedback --- ...ageDiscovery.php => DgiImageDiscovery.php} | 37 +++++++------ .../Property/DgiImageDiscoveryProperty.php | 53 +++++++++++++++++++ 2 files changed, 74 insertions(+), 16 deletions(-) rename src/Plugin/search_api/processor/{IslandoraObjectImageDiscovery.php => DgiImageDiscovery.php} (71%) create mode 100644 src/Plugin/search_api/processor/Property/DgiImageDiscoveryProperty.php diff --git a/src/Plugin/search_api/processor/IslandoraObjectImageDiscovery.php b/src/Plugin/search_api/processor/DgiImageDiscovery.php similarity index 71% rename from src/Plugin/search_api/processor/IslandoraObjectImageDiscovery.php rename to src/Plugin/search_api/processor/DgiImageDiscovery.php index 9669be9..37b93ba 100644 --- a/src/Plugin/search_api/processor/IslandoraObjectImageDiscovery.php +++ b/src/Plugin/search_api/processor/DgiImageDiscovery.php @@ -5,19 +5,19 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\dgi_image_discovery\ImageDiscoveryInterface; +use Drupal\dgi_image_discovery\Plugin\search_api\processor\Property\DgiImageDiscoveryProperty; use Drupal\node\NodeInterface; use Drupal\search_api\Datasource\DatasourceInterface; use Drupal\search_api\Item\ItemInterface; use Drupal\search_api\Processor\ProcessorPluginBase; -use Drupal\search_api\Processor\ProcessorProperty; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Get the styled image url for the islandora node. * * @SearchApiProcessor( - * id = "islandora_object_image_discovery", - * label = @Translation("Islandora Object Image Discovery"), + * id = "dgi_image_discovery", + * label = @Translation("DGI Image Discovery"), * description = @Translation("Get the styled image url for the islandora node."), * stages = { * "add_properties" = 0, @@ -26,7 +26,7 @@ * hidden = true, * ) */ -class IslandoraObjectImageDiscovery extends ProcessorPluginBase implements ContainerFactoryPluginInterface { +class DgiImageDiscovery extends ProcessorPluginBase implements ContainerFactoryPluginInterface { /** * The entity type manager service. @@ -79,13 +79,13 @@ public function getPropertyDefinitions(DatasourceInterface $datasource = NULL) { if (!$datasource) { $definition = [ - 'label' => $this->t('Islandora Object Image Discovery'), + 'label' => $this->t('DGI 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); + $properties['dgi_image_discovery'] = new DgiImageDiscoveryProperty($definition); } return $properties; @@ -101,20 +101,25 @@ public function addFieldValues(ItemInterface $item) { // Get the image discovery URL. if (!$entity->isNew() && $entity instanceof NodeInterface) { $event = $this->imageDiscovery->getImage($entity); - if ($event->hasMedia()) { - $image = $event->getMedia()->field_media_image; + $media = $event->getMedia(); + if (!empty($media)) { + $media_source = $media->getSource(); + $file_id = $media_source->getSourceFieldValue($media); + $image = $this->entityTypeManager->getStorage('file')->load($file_id); + } + + $fields = $item->getFields(FALSE); + $fields = $this->getFieldsHelper()->filterForPropertyPath($fields, NULL, 'dgi_image_discovery'); + foreach ($fields as $field) { + $config = $field->getConfiguration(); + $image_style = $config['image_style']; if (!empty($image)) { - $value = $this->entityTypeManager->getStorage('image_style')->load('solr_grid_thumbnail') - ->buildUrl($image->entity->getFileUri()); + $value = $this->entityTypeManager->getStorage('image_style')->load($image_style) + ->buildUrl($image->getFileUri()); } + $field->addValue($value); } } - - $fields = $item->getFields(FALSE); - $fields = $this->getFieldsHelper()->filterForPropertyPath($fields, NULL, 'islandora_object_image_discovery'); - foreach ($fields as $field) { - $field->addValue($value); - } } } diff --git a/src/Plugin/search_api/processor/Property/DgiImageDiscoveryProperty.php b/src/Plugin/search_api/processor/Property/DgiImageDiscoveryProperty.php new file mode 100644 index 0000000..5ffdbd1 --- /dev/null +++ b/src/Plugin/search_api/processor/Property/DgiImageDiscoveryProperty.php @@ -0,0 +1,53 @@ + 'solr_grid_thumbnail', + ]; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(FieldInterface $field, array $form, FormStateInterface $form_state) { + $configuration = $field->getConfiguration(); + + // Get an array of image styles. + $image_styles_array = \Drupal::entityTypeManager()->getStorage('image_style')->loadMultiple(); + $image_styles = []; + + if (!empty($image_styles_array)) { + foreach ($image_styles_array as $id => $style) { + $image_styles[$id] = $style->label(); + } + } + + $form['image_style'] = [ + '#type' => 'select', + '#options' => $image_styles, + '#title' => $this->t('Image Style'), + '#description' => $this->t('Select the image style that should be applied to derive the DGI Image Discovery image url.'), + '#default_value' => $configuration['image_style'] ?? $this->defaultConfiguration()['image_style'], + ]; + + return $form; + } + +} From 69ec96dc0e8451191f574266020fba27bf11bfb0 Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Fri, 23 Feb 2024 13:43:47 +0530 Subject: [PATCH 4/5] PER-10: Added config schema and updated how image style options are built --- config/schema/search_api.processor.schema.yml | 7 +++++++ .../Property/DgiImageDiscoveryProperty.php | 16 +++------------- 2 files changed, 10 insertions(+), 13 deletions(-) create mode 100644 config/schema/search_api.processor.schema.yml diff --git a/config/schema/search_api.processor.schema.yml b/config/schema/search_api.processor.schema.yml new file mode 100644 index 0000000..2a07257 --- /dev/null +++ b/config/schema/search_api.processor.schema.yml @@ -0,0 +1,7 @@ +search_api.property_configuration.dgi_image_discovery: + type: mapping + label: DGI Image Discovery + mapping: + image_style: + type: string + label: 'The image style to be used to create image url' diff --git a/src/Plugin/search_api/processor/Property/DgiImageDiscoveryProperty.php b/src/Plugin/search_api/processor/Property/DgiImageDiscoveryProperty.php index 5ffdbd1..3a22b30 100644 --- a/src/Plugin/search_api/processor/Property/DgiImageDiscoveryProperty.php +++ b/src/Plugin/search_api/processor/Property/DgiImageDiscoveryProperty.php @@ -27,24 +27,14 @@ public function defaultConfiguration() { * {@inheritdoc} */ public function buildConfigurationForm(FieldInterface $field, array $form, FormStateInterface $form_state) { - $configuration = $field->getConfiguration(); - - // Get an array of image styles. - $image_styles_array = \Drupal::entityTypeManager()->getStorage('image_style')->loadMultiple(); - $image_styles = []; - - if (!empty($image_styles_array)) { - foreach ($image_styles_array as $id => $style) { - $image_styles[$id] = $style->label(); - } - } + $configuration = $field->getConfiguration() + $this->defaultConfiguration(); $form['image_style'] = [ '#type' => 'select', - '#options' => $image_styles, + '#options' => image_style_options(FALSE), '#title' => $this->t('Image Style'), '#description' => $this->t('Select the image style that should be applied to derive the DGI Image Discovery image url.'), - '#default_value' => $configuration['image_style'] ?? $this->defaultConfiguration()['image_style'], + '#default_value' => $configuration['image_style'], ]; return $form; From 7435720afab86fd4a147ccd84bdadca70f8120ef Mon Sep 17 00:00:00 2001 From: axelerant-hardik Date: Fri, 23 Feb 2024 21:09:39 +0530 Subject: [PATCH 5/5] PER-10: Better code readability --- .../processor/DgiImageDiscovery.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/Plugin/search_api/processor/DgiImageDiscovery.php b/src/Plugin/search_api/processor/DgiImageDiscovery.php index 37b93ba..b0674f8 100644 --- a/src/Plugin/search_api/processor/DgiImageDiscovery.php +++ b/src/Plugin/search_api/processor/DgiImageDiscovery.php @@ -102,10 +102,15 @@ public function addFieldValues(ItemInterface $item) { if (!$entity->isNew() && $entity instanceof NodeInterface) { $event = $this->imageDiscovery->getImage($entity); $media = $event->getMedia(); - if (!empty($media)) { - $media_source = $media->getSource(); - $file_id = $media_source->getSourceFieldValue($media); - $image = $this->entityTypeManager->getStorage('file')->load($file_id); + if (empty($media)) { + return; + } + + $media_source = $media->getSource(); + $file_id = $media_source->getSourceFieldValue($media); + $image = $this->entityTypeManager->getStorage('file')->load($file_id); + if (empty($image)) { + return; } $fields = $item->getFields(FALSE); @@ -113,10 +118,8 @@ public function addFieldValues(ItemInterface $item) { foreach ($fields as $field) { $config = $field->getConfiguration(); $image_style = $config['image_style']; - if (!empty($image)) { - $value = $this->entityTypeManager->getStorage('image_style')->load($image_style) - ->buildUrl($image->getFileUri()); - } + $value = $this->entityTypeManager->getStorage('image_style')->load($image_style) + ->buildUrl($image->getFileUri()); $field->addValue($value); } }