Issue #3421114 by tstoeckler: [regression] Entity::toUrl() without argument is broken for entity types with a URI callback

merge-requests/6604/merge
Lee Rowlands 2024-02-15 09:59:01 +10:00
parent 54f3e98a56
commit c51465ddb0
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
2 changed files with 13 additions and 8 deletions

View File

@ -164,6 +164,13 @@ abstract class EntityBase implements EntityInterface {
// 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 @@ abstract class EntityBase implements EntityInterface {
$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 @@ abstract class EntityBase implements EntityInterface {
$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);
}
}

View File

@ -120,6 +120,7 @@ class EntityUrlTest extends UnitTestCase {
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 @@ class EntityUrlTest extends UnitTestCase {
/** @var \Drupal\Core\Url $url */
$url = $entity->toUrl('canonical');
$this->assertUrl('<none>', [], $entity, TRUE, $url);
$url = $entity->toUrl();
$this->assertUrl('<none>', [], $entity, TRUE, $url);
}
/**