Problem/Motivation
The signature for EntityInterface::label()
:
/**
* Gets the label of the entity.
*
* @return string|\Drupal\Core\StringTranslation\TranslatableMarkup|null
* The label of the entity, or NULL if there is no label defined.
*/
public function label();
It allows an entity to return NULL as a label, which basically means that the entity does not have a label at all. EntityController uses this method for editing and deleting form page titles.
- \Drupal\Core\Entity\Controller\EntityController::editTitle
- \Drupal\Core\Entity\Controller\EntityController::deleteTitle
What it does is:
return $this->t('Edit %label', ['%label' => $entity->label()]);
But since
this change β
in Drupal 11, it is not allowed to use a NULL
value as an argument value. That means these pages become unavailable because of this.
Steps to reproduce
Create an entity with a label with a NULL
value.
I've tried to create a fail test, and found that the entity_test
entity type always gets its random label from somewhere else, even if the name
field is explicitly set to NULL
. This is likely an issue with the core tests, which are not detecting it. Not sure where to approach here.
Proposed resolution
Handle the NULL
label in those methods. Something like:
public function editTitle(RouteMatchInterface $route_match, ?EntityInterface $_entity = NULL) {
if ($entity = $this->doGetEntity($route_match, $_entity)) {
$label = $entity->label() ?? $entity->getEntityType()->getSingularLabel();
return $this->t('Edit %label', ['%label' => $label]);
}
}