An entity without a label causes an uncaught exception for the navigation:title component

Created on 7 February 2025, about 2 months ago

Problem/Motivation

When viewing the page of an entity which doesn't have a label, the

navigation:title</component> throws the following:

<code>
Drupal\Core\Render\Component\Exception\InvalidComponentDataException: Unable to render component "navigation:title". A render array or a scalar is expected for the slot "content" when using the render element with the "#slots" property in Drupal\Core\Render\Element\ComponentElement->generateComponentTemplate() (line 118 of core/lib/Drupal/Core/Render/Element/ComponentElement.php).

This comes from PageContext which has:

    $build += [
      [
        '#type' => 'component',
        '#component' => 'navigation:title',
        '#props' => [
          'icon' => 'database',
          'html_tag' => 'span',
          'modifiers' => ['ellipsis', 'xs'],
          'extra_classes' => ['top-bar__title'],
        ],
        '#slots' => [
          'content' => $entity->label(),
        ],
      ],
    ];

This doesn't account for the fact that EntityInterface::label can legitimately return NULL:

  /**
   * 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();

Steps to reproduce

View an entity without a label entity key/custom label() implementation.

Proposed resolution

Not sure if a slot can validly be defined to allow NULLs, or whether PageContext should perform some checks before attempting to render the component.

Remaining tasks

User interface changes

N/A

Introduced terminology

N/A

API changes

Data model changes

N/A

Release notes snippet

🐛 Bug report
Status

Active

Version

11.1 🔥

Component

single-directory components

Created by

🇬🇧United Kingdom andrewbelcher

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024