From 9350c4fe44321dd97e1dfdd681d7ae5bca805749 Mon Sep 17 00:00:00 2001 From: Jordan Dukart Date: Thu, 14 Sep 2023 14:34:07 -0300 Subject: [PATCH] Trait for URI generation + reworks. --- iiif_presentation_api.services.yml | 2 +- src/Normalizer/EntityUriTrait.php | 53 +++++++++++++++++++ src/Normalizer/RouterProviderTrait.php | 41 ++++++++++++++ src/Normalizer/V3/ContentEntityNormalizer.php | 15 ++++-- src/Normalizer/V3/NormalizerBase.php | 39 -------------- 5 files changed, 106 insertions(+), 44 deletions(-) create mode 100644 src/Normalizer/EntityUriTrait.php create mode 100644 src/Normalizer/RouterProviderTrait.php diff --git a/iiif_presentation_api.services.yml b/iiif_presentation_api.services.yml index 05b3c1e..51bcd3e 100644 --- a/iiif_presentation_api.services.yml +++ b/iiif_presentation_api.services.yml @@ -7,7 +7,7 @@ services: class: 'Drupal\iiif_presentation_api\Normalizer\V3\ContentEntityNormalizer' tags: - { name: normalizer, priority: 10 } - arguments: ['@current_user'] + arguments: ['@current_user', '@router.route_provider'] serializer.normalizer.iiif_presentation_api.iiif_p_v3.field_item_list: class: 'Drupal\iiif_presentation_api\Normalizer\V3\FieldItemListNormalizer' tags: diff --git a/src/Normalizer/EntityUriTrait.php b/src/Normalizer/EntityUriTrait.php new file mode 100644 index 0000000..f3b3930 --- /dev/null +++ b/src/Normalizer/EntityUriTrait.php @@ -0,0 +1,53 @@ +isNew()) { + return ''; + } + + $route_name = 'rest.entity.' . $entity->getEntityTypeId() . '.GET'; + if ($entity->hasLinkTemplate('canonical')) { + $url = $entity->toUrl('canonical'); + } + elseif ($this->getRouteProvider()->getRoutesByNames([$route_name])) { + $url = Url::fromRoute('rest.entity.' . $entity->getEntityTypeId() . '.GET', [$entity->getEntityTypeId() => $entity->id()]); + } + else { + return ''; + } + + $url->setAbsolute(); + if (!$url->isExternal()) { + $url->setRouteParameter('_format', 'iiif-p-v3'); + } + $generated_url = $url->toString(TRUE); + $this->addCacheableDependency($context, $generated_url); + + return $generated_url->getGeneratedUrl(); + } + +} diff --git a/src/Normalizer/RouterProviderTrait.php b/src/Normalizer/RouterProviderTrait.php new file mode 100644 index 0000000..0024487 --- /dev/null +++ b/src/Normalizer/RouterProviderTrait.php @@ -0,0 +1,41 @@ +routeProvider)) { + $this->routeProvider = \Drupal::service('router.route_provider'); + } + return $this->routeProvider; + } + + /** + * Set the route provider service. + * + * @param \Drupal\Core\Routing\RouteProviderInterface $routeProvider + * The route provider service. + */ + protected function setRouteProvider(RouteProviderInterface $routeProvider): void { + $this->routeProvider = $routeProvider; + } + +} diff --git a/src/Normalizer/V3/ContentEntityNormalizer.php b/src/Normalizer/V3/ContentEntityNormalizer.php index 0718788..628d92c 100644 --- a/src/Normalizer/V3/ContentEntityNormalizer.php +++ b/src/Normalizer/V3/ContentEntityNormalizer.php @@ -4,13 +4,17 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\Session\AccountInterface; +use Drupal\iiif_presentation_api\Normalizer\EntityUriTrait; /** * Normalizer for content entities. */ class ContentEntityNormalizer extends NormalizerBase { + use EntityUriTrait; + /** * {@inheritDoc} */ @@ -28,9 +32,12 @@ class ContentEntityNormalizer extends NormalizerBase { * * @param \Drupal\Core\Session\AccountInterface $user * The current user. + * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider + * The route provider service. */ - public function __construct(AccountInterface $user) { + public function __construct(AccountInterface $user, RouteProviderInterface $route_provider) { $this->user = $user; + $this->setRouteProvider($route_provider); } /** @@ -39,7 +46,7 @@ public function __construct(AccountInterface $user) { public function normalize($object, $format = NULL, array $context = []) { $normalized = []; - if (empty($context)) { + if (!isset($context['base-depth'])) { $context['base-depth'] = TRUE; $normalized['@context'] = 'http://iiif.io/api/presentation/3/context.json'; } @@ -50,7 +57,7 @@ public function normalize($object, $format = NULL, array $context = []) { 'id' => $this->getEntityUri($object, $context), 'type' => $context['base-depth'] ? 'Manifest' : 'Canvas', 'label' => [ - $object->language()->getId() => [$object->label()], + 'none' => [$object->label()], ], ]; @@ -84,7 +91,7 @@ public function normalize($object, $format = NULL, array $context = []) { * @return array * The normalized representation of the entity. */ - protected function normalizeEntityFields(ContentEntityInterface $object, string $format, array $context, array $normalized) { + protected function normalizeEntityFields(ContentEntityInterface $object, string $format, array $context, array $normalized): array { $this->addCacheableDependency($context, $object); foreach ($object->getFields() as $field) { $access = $field->access('view', $context['account'], TRUE); diff --git a/src/Normalizer/V3/NormalizerBase.php b/src/Normalizer/V3/NormalizerBase.php index 51bceff..7fabc1e 100644 --- a/src/Normalizer/V3/NormalizerBase.php +++ b/src/Normalizer/V3/NormalizerBase.php @@ -2,8 +2,6 @@ namespace Drupal\iiif_presentation_api\Normalizer\V3; -use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Url; use Drupal\serialization\Normalizer\NormalizerBase as SerializationNormalizerBase; /** @@ -41,43 +39,6 @@ protected function checkFormat($format = NULL) { return $format === $this->format; } - /** - * Constructs the entity URI. - * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity. - * @param array $context - * Normalization/serialization context. - * - * @return string - * The entity URI. - */ - protected function getEntityUri(EntityInterface $entity, array $context = []) { - // Some entity types don't provide a canonical link template. - if ($entity->isNew()) { - return ''; - } - - $route_name = 'rest.entity.' . $entity->getEntityTypeId() . '.GET'; - if ($entity->hasLinkTemplate('canonical')) { - $url = $entity->toUrl('canonical'); - } - elseif (\Drupal::service('router.route_provider')->getRoutesByNames([$route_name])) { - $url = Url::fromRoute('rest.entity.' . $entity->getEntityTypeId() . '.GET', [$entity->getEntityTypeId() => $entity->id()]); - } - else { - return ''; - } - - $url->setAbsolute(); - if (!$url->isExternal()) { - $url->setRouteParameter('_format', 'iiif-p-v3'); - } - $generated_url = $url->toString(TRUE); - $this->addCacheableDependency($context, $generated_url); - return $generated_url->getGeneratedUrl(); - } - /** * {@inheritdoc} */