EntityController regression with NULL entity label on Drupal 11

Created on 21 October 2024, 2 months ago

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.

  1. \Drupal\Core\Entity\Controller\EntityController::editTitle
  2. \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]);
    }
  }
πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component

entity system

Created by

πŸ‡·πŸ‡ΊRussia niklan Russia, Perm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024