-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf92e90
commit 9350c4f
Showing
5 changed files
with
106 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters