Quantcast
Channel: Issues for Drupal core
Viewing all articles
Browse latest Browse all 295840

getEntityTypeLabels uses translatable strings as array keys

$
0
0

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'])];

Viewing all articles
Browse latest Browse all 295840

Trending Articles