diff --git a/core/lib/Drupal/Core/Entity/EntityBase.php b/core/lib/Drupal/Core/Entity/EntityBase.php index b05468cdca87719af3d58c7f0e9e25acc270baf0..781417760ce6ec6b12f7a58b13bb15887d66481d 100644 --- a/core/lib/Drupal/Core/Entity/EntityBase.php +++ b/core/lib/Drupal/Core/Entity/EntityBase.php @@ -164,6 +164,13 @@ public function toUrl($rel = NULL, array $options = []) { // The links array might contain URI templates set in annotations. $link_templates = $this->linkTemplates(); + // Links pointing to the current revision point to the actual entity. So + // instead of using the 'revision' link, use the 'canonical' link. + if ($rel === 'revision' && $this instanceof RevisionableInterface && $this->isDefaultRevision()) { + $rel = 'canonical'; + } + + $exception_message = "No link template '$rel' found for the '{$this->getEntityTypeId()}' entity type"; // Use the canonical link template by default, or edit-form if there is not // a canonical one. if ($rel === NULL) { @@ -174,16 +181,10 @@ public function toUrl($rel = NULL, array $options = []) { $rel = 'edit-form'; } else { - throw new UndefinedLinkTemplateException("Cannot generate default URL because no link template 'canonical' or 'edit-form' was found for the '{$this->getEntityTypeId()}' entity type"); + $exception_message = "Cannot generate default URL because no link template 'canonical' or 'edit-form' was found for the '{$this->getEntityTypeId()}' entity type"; } } - // Links pointing to the current revision point to the actual entity. So - // instead of using the 'revision' link, use the 'canonical' link. - if ($rel === 'revision' && $this instanceof RevisionableInterface && $this->isDefaultRevision()) { - $rel = 'canonical'; - } - if (isset($link_templates[$rel])) { $route_parameters = $this->urlRouteParameters($rel); $route_name = "entity.{$this->entityTypeId}." . str_replace(['-', 'drupal:'], ['_', ''], $rel); @@ -207,7 +208,7 @@ public function toUrl($rel = NULL, array $options = []) { $uri = call_user_func($uri_callback, $this); } else { - throw new UndefinedLinkTemplateException("No link template '$rel' found for the '{$this->getEntityTypeId()}' entity type"); + throw new UndefinedLinkTemplateException($exception_message); } } diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php index 8e1bf3b2efaa80b13f286150ca8a66c0612584ff..d6b7c2f9d722c4167d137c55e0571d95b3cac6f5 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php @@ -120,6 +120,7 @@ public function testToUrlNoId() { public function testToUrlDefaultException(): void { $values = ['id' => $this->entityId]; $entity = $this->getEntity(UrlTestEntity::class, $values); + $this->entityType->getUriCallback()->willReturn(NULL); $this->expectException(UndefinedLinkTemplateException::class); $this->expectExceptionMessage("Cannot generate default URL because no link template 'canonical' or 'edit-form' was found for the '" . $this->entityTypeId . "' entity type"); @@ -409,6 +410,9 @@ public function testToUrlUriCallback(array $bundle_info, $uri_callback) { /** @var \Drupal\Core\Url $url */ $url = $entity->toUrl('canonical'); $this->assertUrl('<none>', [], $entity, TRUE, $url); + + $url = $entity->toUrl(); + $this->assertUrl('<none>', [], $entity, TRUE, $url); } /**