Field type list_string tokens are not translated, always original language

Created on 23 December 2020, over 4 years ago
Updated 12 September 2024, 7 months ago

Problem/Motivation

When using a token of type [entity_type:field_name] on an entity field of type list_string, \Drupal::token()->replace() will always resolve to the site's default language even if passing the langcode parameter to that method.

Steps to reproduce

Having a content entity with a list_string field such as:

$fields['test_list_field'] = BaseFieldDefinition::create('list_string')
      ->setLabel(t('Test list field'))
      ->setRequired(TRUE)
      ->setSetting('allowed_values', [
        'option_1' => t('First option'),
        'option_2' => t('Second option'),
      ]);

Assuming these field options are already translated, \Drupal::token()->replace(), will always resolve to the same language even if language context parameter is set.

Examples:

\Drupal::token()->replace('[test_entity:test_list_field]', [
      'test_entity' => $entity,
    ]); // 'First option'. OK.
\Drupal::token()->replace('[test_entity:test_list_field]', [
      'test_entity' => $entity,
    ],
    [
      'langcode' => 'es',
    ]); // 'First option' ('Primera opciΓ³n' expected).
πŸ› Bug report
Status

Needs work

Version

1.0

Component

Code

Created by

πŸ‡¨πŸ‡΄Colombia ibustos BogotΓ‘ D.C.

Live updates comments and jobs are added and updated live.
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.

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Just ran into the same issue. We needed the value from a list_string field to build the URL alias. The list_string values are all translated, but the tokens result is always returned in the original language.

    Setting the priority to Major as it breaks expected functionality for translations and there's no workaround.

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Adding more or less related issues on translation.

  • πŸ‡«πŸ‡·France duaelfr Montpellier, France

    Same issue here using a list_string type field with translated labels supposed to be used by pathauto to generate the path of the node.
    That part of the path is always in the default language.

    I traced that down to the $entity->$field_name->view($display_options); call in _field_tokens() where I found something really strange happening (which seem to be a Core issue).

    $entity->$field_name->language()->getId() returns the appropriate language.
    $entity->$field_name->getEntity()->language()->getId() (first thing done by the \Drupal\Core\Entity\EntityViewBuilder::viewField() method) returns the default language!

    I didn't find anything related to that issue in the issue queue and I'm not sure I know how to file that. Any help would be appreciated.

  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    @duaelfr reading this I think it might be by design as $entity->$field_name->getEntity() might return the "original" entity (not the translation of it), so back there it might need ->hasTranslation() (https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!ContentEnt...) again like you never had the language.

    So it might be expected and by design, but unintuitive. So essentially the language argument then seems to be missing somewhere over there?

  • πŸ‡«πŸ‡·France duaelfr Montpellier, France

    @anybody You must be right about getEntity() as the \Drupal\Core\Entity\EntityViewBuilder::viewField() method calls \Drupal\Core\Entity\EntityRepositoryInterface::getTranslationFromContext() right after. The thing is that it does the call only for untranslatable fields and without any langcode so it fallbacks on the content language which is not the one we expect in most technical cases.
    In my case, the field is not translatable so it wouldn't work anyway… I'll give a try to the #7 patch even if it looks a but hackish.

  • πŸ‡«πŸ‡·France duaelfr Montpellier, France
  • πŸ‡©πŸ‡ͺGermany Anybody Porta Westfalica

    Mhm unsure, both look quite old, so might be unrelated. I'm still not sure if the issue is here in the token implementation or in core. Sadly I don't have time for deeper investigations currently, sorry.

    I think it would be best, if you could find similar places or calls on Core or if we had a clean MR for token to resolve this.
    If that's not possible due to Core API design, I'd vote for a new core issue. Or you could try to discuss this in slack perhaps?

Production build 0.71.5 2024