Skip to content

Commit

Permalink
Merge pull request #68 from adam-vessey/8.x-1.1.1-exception-avoidance
Browse files Browse the repository at this point in the history
Hotfix: Skip link headers for non-canonicalizable entities
  • Loading branch information
jordandukart authored Jun 26, 2020
2 parents 77afd8d + 71863e1 commit 4c2cb5a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/EventSubscriber/LinkHeaderSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
// <url>; rel="tag"; title="Tag Name"
Expand Down
14 changes: 8 additions & 6 deletions src/IslandoraUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,17 @@ private function getEntityQueryOrCondition(QueryInterface $query, array $fields,
*
* @return string
* The entity URL.
*
* @throws \Drupal\Core\Entity\Exception\UndefinedLinkTemplateException
* Thrown if the given entity does not specify a "canonical" template.
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
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();
}

/**
Expand Down

0 comments on commit 4c2cb5a

Please sign in to comment.