From d610e0a327ec8488b3562d5df254bac245edde04 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Wed, 20 Dec 2023 18:37:45 +0530 Subject: [PATCH 1/8] DGIR-142 : Add thumbnail to manifest --- src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php b/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php index 972b102..30c1122 100644 --- a/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php +++ b/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php @@ -72,8 +72,9 @@ protected function getBody(?string $slug, FileInterface $file, array $extra = [] $base_id = strtr($slug, [ '{identifier}' => rawurlencode($id_plugin->getIdentifier($file)), ]); + return [ - 'id' => "$base_id/full/full/0/default.jpg", + 'id' => "$base_id/full/0/default.jpg", 'type' => 'Image', 'format' => 'image/jpeg', 'service' => [ @@ -82,6 +83,10 @@ protected function getBody(?string $slug, FileInterface $file, array $extra = [] 'id' => $base_id, ] + $extra, ], + 'thumbnail' => [ + 'id' => "$base_id/full/255,/0/default.jpg", + 'type' => "dctypes:Image" + ] ]; } From 84a5cdce43d03659358a4e4558ceaa789b088c94 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Wed, 20 Dec 2023 23:56:30 +0530 Subject: [PATCH 2/8] DGIR-142 : Change TNs location and make it an array --- src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php b/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php index 30c1122..6507b37 100644 --- a/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php +++ b/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php @@ -84,9 +84,11 @@ protected function getBody(?string $slug, FileInterface $file, array $extra = [] ] + $extra, ], 'thumbnail' => [ - 'id' => "$base_id/full/255,/0/default.jpg", - 'type' => "dctypes:Image" - ] + [ + 'id' => "$base_id/full/255,/0/default.jpg", + 'type' => "Image" + ], + ], ]; } From 715c31713f0d1ae18cdc84973bea5b050472538b Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Wed, 20 Dec 2023 23:58:58 +0530 Subject: [PATCH 3/8] DGIR-142 : Linting error --- src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php b/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php index 6507b37..b19cd96 100644 --- a/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php +++ b/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php @@ -86,7 +86,7 @@ protected function getBody(?string $slug, FileInterface $file, array $extra = [] 'thumbnail' => [ [ 'id' => "$base_id/full/255,/0/default.jpg", - 'type' => "Image" + 'type' => "Image", ], ], ]; From 7426a5a4dbd7fa32ec326d06b66217d1240864db Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Thu, 21 Dec 2023 00:22:21 +0530 Subject: [PATCH 4/8] DGIR-142 : revert unwanted change --- src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php b/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php index b19cd96..324c489 100644 --- a/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php +++ b/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php @@ -74,7 +74,7 @@ protected function getBody(?string $slug, FileInterface $file, array $extra = [] ]); return [ - 'id' => "$base_id/full/0/default.jpg", + 'id' => "$base_id/full/full/0/default.jpg", 'type' => 'Image', 'format' => 'image/jpeg', 'service' => [ From adb6a0d538f19fa89750a5a25fb63d20dee7404a Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Fri, 22 Dec 2023 13:02:28 +0530 Subject: [PATCH 5/8] DGIR-142 : Update manifest to include thumbnail --- .../V3/BaseImageBodyEventSubscriber.php | 6 --- src/Normalizer/V3/ContentEntityNormalizer.php | 52 +++++++++++++++++++ 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php b/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php index 324c489..0ccfc20 100644 --- a/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php +++ b/src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php @@ -83,12 +83,6 @@ protected function getBody(?string $slug, FileInterface $file, array $extra = [] 'id' => $base_id, ] + $extra, ], - 'thumbnail' => [ - [ - 'id' => "$base_id/full/255,/0/default.jpg", - 'type' => "Image", - ], - ], ]; } diff --git a/src/Normalizer/V3/ContentEntityNormalizer.php b/src/Normalizer/V3/ContentEntityNormalizer.php index f86d0ab..2892586 100644 --- a/src/Normalizer/V3/ContentEntityNormalizer.php +++ b/src/Normalizer/V3/ContentEntityNormalizer.php @@ -6,7 +6,9 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; +use Drupal\file\FileInterface; use Drupal\iiif_presentation_api\Event\V3\ContentEntityExtrasEvent; +use Drupal\iiif_presentation_api\Event\V3\ImageBodyEvent; use Drupal\iiif_presentation_api\Normalizer\EntityUriTrait; use Drupal\node\NodeInterface; use Psr\EventDispatcher\EventDispatcherInterface; @@ -106,9 +108,59 @@ public function normalize($object, $format = NULL, array $context = []) { $normalized = NestedArray::mergeDeep($normalized, $extras); } + // It triggers for node and media. Adds thumbnail multiple time. + // Restricting it for node only. + if ($object->getEntityTypeId() == 'node') { + + // Load the entity type manager. + $entity_type_manager = \Drupal::entityTypeManager(); + + // Get the storage handler for the media entity. + $media_storage = $entity_type_manager->getStorage('media'); + + // Load a single media entity by properties. + $media_entities = $media_storage->loadByProperties([ + 'field_media_use' => 349, // Need to make it dynamic after verification. + 'field_media_of' => $object->id(), + ]); + + // Check if a media entity was found. + if ($media_entities) { + // Process the single media entity as needed. + $media_entity = reset($media_entities); + + // Get the file ID from the file field. + $file_id = $media_entity->get('field_media_image')->target_id; + $file = $entity_type_manager->getStorage('file')->load($file_id); + } + + $normalized['thumbnail'] = $this->generateBody($file); + } return $this->normalizeEntityFields($object, $format, $context, $normalized); } + /** + * Generate the annotation body. + * + * @param \Drupal\file\FileInterface $file + * The file for which to generate the body. + * + * @return array + * An associative array representing the body. + */ + protected function generateBody(FileInterface $file) : array { + /** @var \Drupal\iiif_presentation_api\Event\V3\ImageBodyEvent $event */ + $event_dispatcher = \Drupal::service('event_dispatcher'); + $event = $event_dispatcher->dispatch(new ImageBodyEvent($file)); + $bodies = $event->getBodies(); + if (!$bodies) { + return []; + } + $body = reset($bodies); + $body['service'] = array_merge(...array_filter(array_column($bodies, 'service'))); + return $body; + } + /** * Normalizes all fields present on a content entity. * From 306d40f7d498259231ad9c3e8494893cc850ea8b Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Fri, 22 Dec 2023 14:22:51 +0530 Subject: [PATCH 6/8] DGIR-142 : fix linting error --- src/Normalizer/V3/ContentEntityNormalizer.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Normalizer/V3/ContentEntityNormalizer.php b/src/Normalizer/V3/ContentEntityNormalizer.php index 2892586..48400ea 100644 --- a/src/Normalizer/V3/ContentEntityNormalizer.php +++ b/src/Normalizer/V3/ContentEntityNormalizer.php @@ -150,8 +150,7 @@ public function normalize($object, $format = NULL, array $context = []) { */ protected function generateBody(FileInterface $file) : array { /** @var \Drupal\iiif_presentation_api\Event\V3\ImageBodyEvent $event */ - $event_dispatcher = \Drupal::service('event_dispatcher'); - $event = $event_dispatcher->dispatch(new ImageBodyEvent($file)); + $event = $this->eventDispatcher->dispatch(new ImageBodyEvent($file)); $bodies = $event->getBodies(); if (!$bodies) { return []; From 5ba121e350493d1cf33adc05a7989ae133450fb8 Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Fri, 22 Dec 2023 15:13:04 +0530 Subject: [PATCH 7/8] DGIR-142 : fix linting error --- src/Normalizer/V3/ContentEntityNormalizer.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Normalizer/V3/ContentEntityNormalizer.php b/src/Normalizer/V3/ContentEntityNormalizer.php index 48400ea..2c0fade 100644 --- a/src/Normalizer/V3/ContentEntityNormalizer.php +++ b/src/Normalizer/V3/ContentEntityNormalizer.php @@ -115,12 +115,25 @@ public function normalize($object, $format = NULL, array $context = []) { // Load the entity type manager. $entity_type_manager = \Drupal::entityTypeManager(); + // Get term id for Thumbnail Image. + $term_storage = $entity_type_manager->getStorage('taxonomy_term'); + $term = $term_storage->loadByProperties([ + 'name' => 'Thumbnail Image', + 'vid' => 'islandora_media_use', + ]); + + // Check if the term is found. + if (!empty($term)) { + // Get the term ID. + $term_id = reset($term)->id(); + } + // Get the storage handler for the media entity. $media_storage = $entity_type_manager->getStorage('media'); // Load a single media entity by properties. $media_entities = $media_storage->loadByProperties([ - 'field_media_use' => 349, // Need to make it dynamic after verification. + 'field_media_use' => $term_id, // Need to make it dynamic after verification. 'field_media_of' => $object->id(), ]); @@ -132,9 +145,9 @@ public function normalize($object, $format = NULL, array $context = []) { // Get the file ID from the file field. $file_id = $media_entity->get('field_media_image')->target_id; $file = $entity_type_manager->getStorage('file')->load($file_id); - } - $normalized['thumbnail'] = $this->generateBody($file); + $normalized['thumbnail'] = $this->generateBody($file); + } } return $this->normalizeEntityFields($object, $format, $context, $normalized); } From b7ff4d76ed1168e020a9c432510bd1f5bd90c7ca Mon Sep 17 00:00:00 2001 From: Prashant Kanse Date: Fri, 22 Dec 2023 15:14:46 +0530 Subject: [PATCH 8/8] DGIR-142 : fix linting error --- src/Normalizer/V3/ContentEntityNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Normalizer/V3/ContentEntityNormalizer.php b/src/Normalizer/V3/ContentEntityNormalizer.php index 2c0fade..89a14fb 100644 --- a/src/Normalizer/V3/ContentEntityNormalizer.php +++ b/src/Normalizer/V3/ContentEntityNormalizer.php @@ -133,7 +133,7 @@ public function normalize($object, $format = NULL, array $context = []) { // Load a single media entity by properties. $media_entities = $media_storage->loadByProperties([ - 'field_media_use' => $term_id, // Need to make it dynamic after verification. + 'field_media_use' => $term_id, 'field_media_of' => $object->id(), ]);