Skip to content

Commit

Permalink
DGIR-142 : Update manifest to include thumbnail
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashant-bd committed Dec 22, 2023
1 parent 7426a5a commit adb6a0d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
6 changes: 0 additions & 6 deletions src/EventSubscriber/V3/BaseImageBodyEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
],
];
}

Expand Down
52 changes: 52 additions & 0 deletions src/Normalizer/V3/ContentEntityNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit adb6a0d

Please sign in to comment.