Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTDA9-512: IIIF Image API integration #6

Merged
merged 6 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion islandora_iiif_presentation_api.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
51 changes: 33 additions & 18 deletions src/Normalizer/V3/ImageItemNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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';
}
Expand All @@ -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);
Expand All @@ -68,21 +64,40 @@ 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'],
],
],
];
}

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}
*/
Expand Down
7 changes: 4 additions & 3 deletions src/Normalizer/V3/ImageMediaEntityNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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, []);
}

/**
Expand Down