core/lib/Drupal/Core/Entity/Entity.php | 15 ++++++++++++++- .../modules/node/src/Controller/NodeViewController.php | 14 +------------- .../rest/src/Plugin/rest/resource/EntityResource.php | 18 ++++-------------- core/modules/taxonomy/taxonomy.module | 15 +-------------- 4 files changed, 20 insertions(+), 42 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/Entity.php b/core/lib/Drupal/Core/Entity/Entity.php index 2c6dbc1..1f2638f 100644 --- a/core/lib/Drupal/Core/Entity/Entity.php +++ b/core/lib/Drupal/Core/Entity/Entity.php @@ -13,6 +13,7 @@ use Drupal\Core\Link; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; +use Symfony\Component\Routing\Exception\RouteNotFoundException; /** * Defines a base entity class. @@ -323,7 +324,19 @@ protected function urlRouteParameters($rel) { * {@inheritdoc} */ public function uriRelationships() { - return array_keys($this->linkTemplates()); + return array_filter(array_keys($this->linkTemplates()), function ($link_relation_type) { + // It's not guaranteed that every link relation type also has a + // corresponding route. For some, additional modules or configuration may + // be necessary. The interface demands that we only return supported URI + // relationships. + try { + $this->toUrl($link_relation_type)->toString(TRUE)->getGeneratedUrl(); + } + catch (RouteNotFoundException $e) { + return FALSE; + } + return TRUE; + }); } /** diff --git a/core/modules/node/src/Controller/NodeViewController.php b/core/modules/node/src/Controller/NodeViewController.php index ba73ba6..fce50e1 100644 --- a/core/modules/node/src/Controller/NodeViewController.php +++ b/core/modules/node/src/Controller/NodeViewController.php @@ -8,7 +8,6 @@ use Drupal\Core\Render\RendererInterface; use Drupal\Core\Session\AccountInterface; use Symfony\Component\DependencyInjection\ContainerInterface; -use Symfony\Component\Routing\Exception\RouteNotFoundException; /** * Defines a controller to render a single node. @@ -57,17 +56,6 @@ public function view(EntityInterface $node, $view_mode = 'full', $langcode = NUL foreach ($node->uriRelationships() as $rel) { $url = $node->toUrl($rel); - - // It's not guaranteed that every link relation type also has a - // corresponding route. For some, additional modules or configuration may - // be necessary. - try { - $generated_url = $url->toString(); - } - catch (RouteNotFoundException $e) { - continue; - } - // Add link relationships if the user is authenticated or if the anonymous // user has access. Access checking must be done for anonymous users to // avoid traffic to inaccessible pages from web crawlers. For @@ -83,7 +71,7 @@ public function view(EntityInterface $node, $view_mode = 'full', $langcode = NUL $build['#attached']['html_head_link'][] = array( array( 'rel' => $rel, - 'href' => $generated_url, + 'href' => $url->toString(), ), TRUE, ); diff --git a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php index 8ac38e6..db7fdf8 100644 --- a/core/modules/rest/src/Plugin/rest/resource/EntityResource.php +++ b/core/modules/rest/src/Plugin/rest/resource/EntityResource.php @@ -20,7 +20,6 @@ use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\HttpException; -use Symfony\Component\Routing\Exception\RouteNotFoundException; /** * Represents entities as resources. @@ -391,20 +390,11 @@ public function calculateDependencies() { * @see https://tools.ietf.org/html/rfc5988#section-5 */ protected function addLinkHeaders(EntityInterface $entity, Response $response) { - foreach ($entity->getEntityType()->getLinkTemplates() as $relation_name => $link_template) { + foreach ($entity->uriRelationships() as $relation_name) { if ($definition = $this->linkRelationTypeManager->getDefinition($relation_name, FALSE)) { - // It's not guaranteed that every link relation type also has a - // corresponding route. For some, additional modules or configuration - // may be necessary. - try { - $generator_url = $entity->toUrl($relation_name) - ->setAbsolute(TRUE) - ->toString(TRUE); - } - catch (RouteNotFoundException $e) { - continue; - } - + $generator_url = $entity->toUrl($relation_name) + ->setAbsolute(TRUE) + ->toString(TRUE); if ($response instanceof CacheableResponseInterface) { $response->addCacheableDependency($generator_url); } diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index 1511ac8..8088432 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -16,7 +16,6 @@ use Drupal\taxonomy\Entity\Vocabulary; use Drupal\taxonomy\TermInterface; use Drupal\taxonomy\VocabularyInterface; -use Symfony\Component\Routing\Exception\RouteNotFoundException; /** * Denotes that no term in the vocabulary has a parent. @@ -103,23 +102,11 @@ function taxonomy_page_attachments_alter(array &$page) { $route_match = \Drupal::routeMatch(); if ($route_match->getRouteName() == 'entity.taxonomy_term.canonical' && ($term = $route_match->getParameter('taxonomy_term')) && $term instanceof TermInterface) { foreach ($term->uriRelationships() as $rel) { - $url = $term->toUrl($rel); - - // It's not guaranteed that every link relation type also has a - // corresponding route. For some, additional modules or configuration may - // be necessary. - try { - $generated_url = $url->toString(); - } - catch (RouteNotFoundException $e) { - continue; - } - // Set the URI relationships, like canonical. $page['#attached']['html_head_link'][] = array( array( 'rel' => $rel, - 'href' => $generated_url, + 'href' => $term->url($rel), ), TRUE, );