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.

ContentTranslationHandler::entityFormAlter has this line:

$t_args = ['%language' => $languages[$form_langcode]->getName(), '%title' => $entity->label(), '@title' => $title];

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.

The website encountered an unexpected error. Try again later.

TypeError: Drupal\Component\Utility\Html::escape(): Argument #1 ($text) must be of type string, null given, called in /var/www/html/web/core/lib/Drupal/Component/Render/FormattableMarkup.php on line 256

Steps to reproduce

Create an entity without a label.
Go to translate it.

Proposed resolution

Change the code to handle null labels, perhaps something like this?

$label = $entity->label() ?? $entity->getEntityType()->getSingularLabel();
$t_args = ['%language' => $languages[$form_langcode]->getName(), '%title' => $label, '@title' => $title];

Remaining tasks

  • Agree on approach
  • Write test
  • Update code

User interface changes

none

Introduced terminology

none

API changes

none

Data model changes

none

Release notes snippet

none

Issue fork drupal-3486742

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

nicrodgers created an issue. See original summary.

nicrodgers’s picture

Issue summary: View changes
nicrodgers’s picture

Issue summary: View changes

quietone’s picture

Version: 11.1.x-dev » 11.x-dev

Changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.