Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm sorry if this is a duplicate – I couldn't find any existing issue that matched this, but I can't imagine it hasn't come up yet.
Problem
Let's say you have translations enabled for nodes and taxonomy terms and you're accessing a translated node's tags:
echo $node->getTranslation('de')->field_tags[0]->entity->name[0]->value;
This will always print the tag's name in the current page's content language, while I would argue that, when you explicitly access a translation first, you'd expect the taxonomy term to be translated as well.
Proposed resolution
- Decide whether this is actually a problem/bug.
- If so, resolve it by changing the default behavior to have the above code always print the term's German name.
Comments
Comment #2
mkalkbrennerI'm interesed in Gabor's opinion.
Comment #3
BerdirI think this is by design. It might not be perfect, but that's how it is.
Especially for non-translatable fields, there's little that we can do about it. The field doesn't even know the current language of the entity as it has the original.
That's why we changed formatters to pass around the language as an additional argument, and why \Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase::getEntitiesToView exists.
Comment #15
catchClosing this as 'works by design'. Also though - does this not work?