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

Created on 7 February 2025, 8 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

  • Issue created by @andrewbelcher
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand quietone
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

    Just cross-referencing things, I did run into the same error, but for me, โœจ SDC ComponentElement: Transform slots TranslatableMarkup values to #markup instead of throwing an exception Active helps because it's a TranslatableMarkup object, not null.

    I assume SDC has optional slots, but I think this is a navigation issue. I think an label should either fall back to some fallback string like "- No label -" or possibly skip the whole plugin as it can't really provide meaningful information?

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada m4olivei Grimsby, ON

    Good find. Tagging as Needs test, it's not obvious how to reproduce it. I was able to reproduce it in a very hacky way by adding the following implementation of label() to \Drupal\node\Entity\Node, just to illustrate the issue:

    /**
       * {@inheritdoc}
       */
      public function label() {
        return NULL;
      }

    IMO, the plugin should be skipped if there isn't a useful title or badge. We skip the badge, avoiding this issue if there isn't one. We should do the same for the label.

  • Pipeline finished with Success
    7 months ago
    Total: 355s
    #441070
  • ๐Ÿ‡ฎ๐Ÿ‡นItaly viappidu

    Fork is a little bit behind...

    This patch applies to 11.2.0 (tested)

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada xmacinfo Canada

    The Commerce Invoice module encounters this bug.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia abhijith s

    abhijith s โ†’ made their first commit to this issueโ€™s fork.

  • Merge request !12583#3505182: Test case added for fix โ†’ (Open) created by abhijith s
  • Pipeline finished with Failed
    3 months ago
    Total: 159s
    #536785
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada xmacinfo Canada

    Moving to major since this issue essentially blocks displaying content when en entity without label is used.

  • ๐Ÿ‡ญ๐Ÿ‡ทCroatia ivan zugec

    Getting the same error when I view the logs using the AI logging sub-module with Drupal CMS.

    Drupal: 11.2.2

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States tars16

    We are running into this issue when trying to translate content. The updated check for the label didn't solve our issue, however, casting $label to a string got us past our issue. Not sure if this introduces additional translation issues, but so far it has been working.

              '#slots' => [
                'content' => Element::isRenderArray($label) ? $label : (string) $label,
              ],
    

    Tested with 11.2

  • First commit to issue fork.
  • ๐Ÿ‡ช๐Ÿ‡ธSpain plopesc Valladolid

    Added new iteration to latest MR including code from #12

  • Pipeline finished with Success
    about 2 months ago
    Total: 577s
    #579026
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium tijsdeboeck Antwerp ๐Ÿ‡ง๐Ÿ‡ช ๐Ÿ‡ช๐Ÿ‡บ ๐ŸŒŽ

    @plopsec, your MR works perfectly and solved the issue for me.

  • Pipeline finished with Canceled
    14 days ago
    Total: 126s
    #608241
  • Pipeline finished with Failed
    14 days ago
    Total: 100s
    #608242
  • Pipeline finished with Failed
    14 days ago
    Total: 163s
    #608249
  • Pipeline finished with Failed
    14 days ago
    Total: 171s
    #608250
  • Pipeline finished with Failed
    12 days ago
    Total: 347s
    #610250
  • Pipeline finished with Failed
    12 days ago
    Total: 338s
    #610257
  • Pipeline finished with Failed
    12 days ago
    Total: 394s
    #610279
  • Pipeline finished with Failed
    12 days ago
    Total: 288s
    #610295
  • Pipeline finished with Failed
    12 days ago
    Total: 196s
    #610305
  • Pipeline finished with Failed
    12 days ago
    Total: 145s
    #610313
  • Pipeline finished with Failed
    12 days ago
    Total: 170s
    #610315
  • Pipeline finished with Success
    12 days ago
    Total: 959s
    #610321
  • Pipeline finished with Failed
    12 days ago
    Total: 128s
    #610350
  • Pipeline finished with Running
    12 days ago
    #610366
  • Pipeline finished with Success
    12 days ago
    Total: 1179s
    #610390
  • Pipeline finished with Running
    12 days ago
    #610411
  • Pipeline finished with Failed
    12 days ago
    Total: 779s
    #610443
  • Pipeline finished with Failed
    12 days ago
    Total: 536s
    #610451
  • Pipeline finished with Failed
    9 days ago
    Total: 205s
    #612910
  • Pipeline finished with Failed
    9 days ago
    Total: 143s
    #612949
  • Pipeline finished with Failed
    9 days ago
    Total: 188s
    #612966
  • Pipeline finished with Failed
    9 days ago
    Total: 196s
    #612973
  • Pipeline finished with Failed
    9 days ago
    #612976
  • Pipeline finished with Failed
    9 days ago
    Total: 332s
    #612984
  • Pipeline finished with Running
    9 days ago
    #612990
  • Pipeline finished with Failed
    9 days ago
    Total: 156s
    #613012
  • Pipeline finished with Failed
    9 days ago
    Total: 129s
    #613024
  • Pipeline finished with Failed
    9 days ago
    Total: 438s
    #613029
  • Pipeline finished with Failed
    9 days ago
    Total: 639s
    #613041
  • Pipeline finished with Failed
    9 days ago
    Total: 459s
    #613047
  • Pipeline finished with Failed
    8 days ago
    Total: 302s
    #613408
  • Pipeline finished with Failed
    8 days ago
    Total: 342s
    #613420
  • Pipeline finished with Failed
    8 days ago
    Total: 472s
    #613448
  • Pipeline finished with Failed
    8 days ago
    Total: 714s
    #613482
  • Pipeline finished with Failed
    8 days ago
    Total: 246s
    #613535
  • Pipeline finished with Failed
    8 days ago
    Total: 540s
    #613555
  • Pipeline finished with Failed
    8 days ago
    Total: 575s
    #613836
  • Pipeline finished with Failed
    8 days ago
    Total: 584s
    #613878
  • Pipeline finished with Failed
    8 days ago
    Total: 842s
    #613901
  • Pipeline finished with Failed
    8 days ago
    Total: 116s
    #613941
  • Pipeline finished with Failed
    8 days ago
    Total: 507s
    #613949
  • Pipeline finished with Failed
    8 days ago
    Total: 454s
    #613984
  • Pipeline finished with Failed
    8 days ago
    Total: 429s
    #614011
  • Pipeline finished with Failed
    8 days ago
    Total: 149s
    #614046
  • Pipeline finished with Failed
    8 days ago
    Total: 159s
    #614060
  • Pipeline finished with Failed
    8 days ago
    Total: 541s
    #614069
  • Pipeline finished with Failed
    8 days ago
    Total: 194s
    #614082
  • Pipeline finished with Canceled
    8 days ago
    Total: 93s
    #614091
  • Pipeline finished with Failed
    8 days ago
    Total: 241s
    #614093
  • Pipeline finished with Failed
    8 days ago
    Total: 208s
    #614098
  • Pipeline finished with Failed
    8 days ago
    Total: 522s
    #614104
  • Pipeline finished with Failed
    8 days ago
    Total: 181s
    #614121
  • Pipeline finished with Failed
    8 days ago
    Total: 623s
    #614127
  • Pipeline finished with Failed
    8 days ago
    Total: 2090s
    #614153
  • Pipeline finished with Failed
    8 days ago
    Total: 429s
    #614197
  • Pipeline finished with Failed
    8 days ago
    Total: 127s
    #614205
  • Pipeline finished with Failed
    8 days ago
    Total: 616s
    #614208
  • Pipeline finished with Failed
    8 days ago
    Total: 499s
    #614230
  • Pipeline finished with Failed
    8 days ago
    Total: 412s
    #614265
  • Pipeline finished with Failed
    8 days ago
    Total: 522s
    #614281
  • Pipeline finished with Failed
    8 days ago
    Total: 796s
    #614313
  • Pipeline finished with Failed
    8 days ago
    Total: 249s
    #614340
  • Pipeline finished with Failed
    8 days ago
    Total: 201s
    #614350
  • Pipeline finished with Failed
    8 days ago
    Total: 718s
    #614358
Production build 0.71.5 2024