Browse Source

Update means of generating LinkHeader URL.

... allow for catching exceptions for entities without canonical
URLs.
pull/781/head
Adam Vessey 6 years ago
parent
commit
40d0dae1e7
  1. 8
      src/EventSubscriber/LinkHeaderSubscriber.php
  2. 14
      src/IslandoraUtils.php

8
src/EventSubscriber/LinkHeaderSubscriber.php

@ -9,6 +9,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\islandora\IslandoraUtils; use Drupal\islandora\IslandoraUtils;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
@ -205,7 +206,14 @@ abstract class LinkHeaderSubscriber implements EventSubscriberInterface {
// Headers are subject to an access check. // Headers are subject to an access check.
if ($referencedEntity->access('view')) { if ($referencedEntity->access('view')) {
try {
$entity_url = $this->utils->getEntityUrl($referencedEntity); $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 // Taxonomy terms are written out as
// <url>; rel="tag"; title="Tag Name" // <url>; rel="tag"; title="Tag Name"

14
src/IslandoraUtils.php

@ -563,15 +563,17 @@ class IslandoraUtils {
* *
* @return string * @return string
* The entity URL. * The entity URL.
*
* @throws \Drupal\Core\Entity\Exception\UndefinedLinkTemplateException
* Should the given entity not specify a "canonical" template.
*/ */
public function getEntityUrl(EntityInterface $entity) { public function getEntityUrl(EntityInterface $entity) {
$undefined = $this->languageManager->getLanguage('und'); $undefined = $this->languageManager->getLanguage('und');
$entity_type = $entity->getEntityTypeId(); return $entity->toUrl('canonical', [
return Url::fromRoute( 'absolute' => TRUE,
"entity.$entity_type.canonical", 'language' => $undefined,
[$entity_type => $entity->id()], ])
['absolute' => TRUE, 'language' => $undefined] ->toString();
)->toString();
} }
/** /**

Loading…
Cancel
Save