Skip to content

Commit

Permalink
Trait for URI generation + reworks.
Browse files Browse the repository at this point in the history
  • Loading branch information
jordandukart committed Sep 14, 2023
1 parent bf92e90 commit 9350c4f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 44 deletions.
2 changes: 1 addition & 1 deletion iiif_presentation_api.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
53 changes: 53 additions & 0 deletions src/Normalizer/EntityUriTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Drupal\iiif_presentation_api\Normalizer;

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;

/**
* Provides a trait for generating entity URIs.
*/
trait EntityUriTrait {

use RouterProviderTrait;

/**
* 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 = []): string {
// 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 ($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();
}

}
41 changes: 41 additions & 0 deletions src/Normalizer/RouterProviderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Drupal\iiif_presentation_api\Normalizer;

use Drupal\Core\Routing\RouteProviderInterface;

/**
* Provides a trait for injecting the route provider service.
*/
trait RouterProviderTrait {
/**
* The route provider service.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected RouteProviderInterface $routeProvider;

/**
* Get the route provider service.
*
* @return \Drupal\Core\Routing\RouteProviderInterface
* The route provider service.
*/
protected function getRouteProvider(): RouteProviderInterface {
if (!isset($this->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;
}

}
15 changes: 11 additions & 4 deletions src/Normalizer/V3/ContentEntityNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -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);
}

/**
Expand All @@ -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';
}
Expand All @@ -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()],
],
];

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

/**
Expand Down Expand Up @@ -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}
*/
Expand Down

0 comments on commit 9350c4f

Please sign in to comment.