From 79952709bb72af6c73c9673d5f0e73b9f18b2c3d Mon Sep 17 00:00:00 2001 From: Adam Vessey Date: Wed, 24 Jun 2020 15:07:53 -0300 Subject: [PATCH] Update means of generating LinkHeader URL. ... allow for catching exceptions for entities without canonical URLs. --- src/EventSubscriber/LinkHeaderSubscriber.php | 10 +++++++++- src/IslandoraUtils.php | 14 ++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/EventSubscriber/LinkHeaderSubscriber.php b/src/EventSubscriber/LinkHeaderSubscriber.php index efb0e6384..fd5e33722 100644 --- a/src/EventSubscriber/LinkHeaderSubscriber.php +++ b/src/EventSubscriber/LinkHeaderSubscriber.php @@ -9,6 +9,7 @@ use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Session\AccountInterface; use Drupal\islandora\IslandoraUtils; +use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; @@ -205,7 +206,14 @@ protected function generateEntityReferenceLinks(EntityInterface $entity) { // Headers are subject to an access check. if ($referencedEntity->access('view')) { - $entity_url = $this->utils->getEntityUrl($referencedEntity); + try { + $entity_url = $this->utils->getEntityUrl($referencedEntity); + } + catcH (UndefinedLinkTemplateException $e) { + // Not all referencable entities can generate canonical URLs, for + // example: block entities. + continue; + } // Taxonomy terms are written out as // ; rel="tag"; title="Tag Name" diff --git a/src/IslandoraUtils.php b/src/IslandoraUtils.php index 1d3377963..51db0b2bb 100644 --- a/src/IslandoraUtils.php +++ b/src/IslandoraUtils.php @@ -563,15 +563,17 @@ private function getEntityQueryOrCondition(QueryInterface $query, array $fields, * * @return string * The entity URL. + * + * @throws \Drupal\Core\Entity\Exception\UndefinedLinkTemplateException + * Should the given entity not specify a "canonical" template. */ public function getEntityUrl(EntityInterface $entity) { $undefined = $this->languageManager->getLanguage('und'); - $entity_type = $entity->getEntityTypeId(); - return Url::fromRoute( - "entity.$entity_type.canonical", - [$entity_type => $entity->id()], - ['absolute' => TRUE, 'language' => $undefined] - )->toString(); + return $entity->toUrl('canonical', [ + 'absolute' => TRUE, + 'language' => $undefined, + ]) + ->toString(); } /**