diff --git a/composer.json b/composer.json index 2a3281f..e519005 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Provides metadata and canvas information for IIIF Presentation API manifests in Drupal.", "type": "drupal-module", "require": { - "discoverygarden/iiif_presentation_api": "^2", + "discoverygarden/iiif_presentation_api": "^2.1", "islandora/islandora": "^2" }, "license": "GPL-3.0-only" diff --git a/islandora_iiif_presentation_api.services.yml b/islandora_iiif_presentation_api.services.yml index 72c706a..169f0ac 100644 --- a/islandora_iiif_presentation_api.services.yml +++ b/islandora_iiif_presentation_api.services.yml @@ -28,4 +28,6 @@ services: class: 'Drupal\islandora_iiif_presentation_api\Normalizer\V3\ImageItemNormalizer' tags: - { name: normalizer, priority: 25 } - arguments: ['@entity_type.manager'] + arguments: + - '@entity_type.manager' + - '@event_dispatcher' diff --git a/src/Normalizer/V3/ImageItemNormalizer.php b/src/Normalizer/V3/ImageItemNormalizer.php index fd5978b..d5d2132 100644 --- a/src/Normalizer/V3/ImageItemNormalizer.php +++ b/src/Normalizer/V3/ImageItemNormalizer.php @@ -3,10 +3,13 @@ namespace Drupal\islandora_iiif_presentation_api\Normalizer\V3; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\file\FileInterface; +use Drupal\iiif_presentation_api\Event\V3\ImageBodyEvent; use Drupal\iiif_presentation_api\Normalizer\EntityUriTrait; use Drupal\iiif_presentation_api\Normalizer\V3\NormalizerBase; use Drupal\image\Plugin\Field\FieldType\ImageItem; use Drupal\islandora_iiif_presentation_api\Normalizer\FieldItemSpecificNormalizerTrait; +use Psr\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Serializer\Exception\LogicException; /** @@ -23,20 +26,12 @@ class ImageItemNormalizer extends NormalizerBase { protected $supportedInterfaceOrClass = ImageItem::class; /** - * The entity type manager. - * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface + * Constructor. */ - protected EntityTypeManagerInterface $entityTypeManager; - - /** - * Constructor for the ImageItemNormalizer. - * - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager - * The entity type manager. - */ - public function __construct(EntityTypeManagerInterface $entity_type_manager) { - $this->entityTypeManager = $entity_type_manager; + public function __construct( + protected EntityTypeManagerInterface $entityTypeManager, + protected EventDispatcherInterface $eventDispatcher, + ) { $this->supportedReferenceField = 'field_media_image'; $this->supportedEntityType = 'media'; } @@ -58,6 +53,7 @@ public function normalize($object, $format = NULL, array $context = []) { $normalized['width'] = (int) $values['width']; } + /** @var \Drupal\file\FileInterface $file */ $file = $this->entityTypeManager->getStorage('file')->load($values['target_id']); if ($file) { $this->addCacheableDependency($context, $file); @@ -68,11 +64,8 @@ public function normalize($object, $format = NULL, array $context = []) { [ 'id' => $this->getEntityUri($file, $context), 'type' => 'Annotation', - 'body' => [ - 'id' => $file->createFileUrl(FALSE), - 'type' => 'Image', - 'format' => $file->getMimeType(), - ], + 'motivation' => 'painting', + 'body' => $this->generateBody($file), 'height' => (int) $normalized['height'], 'width' => (int) $normalized['width'], 'target' => $context['parent']['id'], @@ -80,9 +73,31 @@ public function normalize($object, $format = NULL, array $context = []) { ], ]; } + return $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 = $this->eventDispatcher->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; + } + /** * {@inheritDoc} */ diff --git a/src/Normalizer/V3/ImageMediaEntityNormalizer.php b/src/Normalizer/V3/ImageMediaEntityNormalizer.php index 7467d89..bfb463d 100644 --- a/src/Normalizer/V3/ImageMediaEntityNormalizer.php +++ b/src/Normalizer/V3/ImageMediaEntityNormalizer.php @@ -5,7 +5,6 @@ use Drupal\iiif_presentation_api\Normalizer\EntityUriTrait; use Drupal\iiif_presentation_api\Normalizer\V3\ContentEntityNormalizer; use Drupal\media\MediaInterface; -use Symfony\Component\Serializer\Exception\LogicException; /** * Normalizer for image media entities. @@ -24,11 +23,13 @@ class ImageMediaEntityNormalizer extends ContentEntityNormalizer { */ public function normalize($object, $format = NULL, array $context = []) { if (!isset($context['parent'])) { - throw new LogicException('Media must be normalized with a parent context.'); + throw new \LogicException('Media must be normalized with a parent context.'); } // XXX: If the parent is already a canvas just pass along the media's fields // to be normalized as opposed to creating a new level / item. - return $context['parent']['type'] === 'Manifest' ? parent::normalize($object, $format, $context) : $this->normalizeEntityFields($object, $format, $context, []); + return $context['parent']['type'] === 'Manifest' ? + parent::normalize($object, $format, $context) : + $this->normalizeEntityFields($object, $format, $context, []); } /**