getEntityTypeLabels uses translatable strings as array keys

Created on 10 August 2022, over 2 years ago
Updated 17 February 2023, almost 2 years ago

Problem/Motivation

Translatable strings shouldn't be used as array keys. They can easily be changed from the UI, causing things to break.
In Drupal\Core\Entity\EntityTypeRepository::getEntityTypeLabels() line 52, we have the following code:

$options[(string) $definition->getGroupLabel()][$entity_type_id] = $definition->getLabel();

$definition->getGroupLabel() returns a translatable string, and shouldn't be used as an array key.

Proposed resolution

We could probably just use $definition->getGroup() as array key instead.

Remaining tasks

The change in core would be fairly simple, but there are a lot of contrib modules depend on this, in the sense that they use code similar to this:

$labels = \Drupal::service('entity_type.repository')->getEntityTypeLabels(TRUE);
$label = $labels[(string) t('Content', [], ['context' => 'Entity type group'])];
πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
EntityΒ  β†’

Last updated 2 days ago

Created by

πŸ‡©πŸ‡°Denmark J_Drupal Copenhagen

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024