Allowed values for list integer field ignores active language

Created on 22 October 2018, about 6 years ago
Updated 10 May 2024, 7 months ago

In 8.5 and earlier, this code would get the translated 'allowed values' for a non translatable list integer field:

$node = $node->getTranslation('cy');
$field = $node->field_guide_type;
$def = $field->getFieldDefinition();
$values = $def->getSetting('allowed_values');

In 8.6 and above, the allowed values are always returned in English (main site language) rather than the translation.

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
Language systemΒ  β†’

Last updated 1 day ago

  • Maintained by
  • πŸ‡©πŸ‡ͺGermany @sun
Created by

πŸ‡¬πŸ‡§United Kingdom nicrodgers Monmouthshire, UK

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.

  • πŸ‡·πŸ‡΄Romania reszli TΓ’rgu MureΘ™

    we ran into this issue as well, with a list<string> on a user profile form
    and we found the following workaround:

    $language_manager = \Drupal::languageManager();
    $current_language = $language_manager->getConfigOverrideLanguage();
    
    $language_manager->setConfigOverrideLanguage($current_language);
    $field_storage = \Drupal::entityTypeManager()
      ->getStorage('field_storage_config')
      ->loadUnchanged('user.[FIELD_NAME]'');
    $allowed_values = $field_storage->getSetting('allowed_values');
    
    // Do your things here.
    // ... i.e. overriding the widget options in the user form
    $form['[FIELD_NAME]'']['widget']['#options'] = $allowed_values;
    
    // Revert to the original language. <== not needed in our case
    // $language_manager->setConfigOverrideLanguage($current_language); 
    

    or this is how it would look like in case of a node field:

    $language_manager = \Drupal::languageManager();
    $current_language = $language_manager->getConfigOverrideLanguage();
    
    // Load translated allowed values for the field by overriding config
    // language temporary.
    $language_manager->setConfigOverrideLanguage($node->language());
    $field_storage = \Drupal::entityTypeManager()
      ->getStorage('field_storage_config')
      ->loadUnchanged('node.[FIELD_NAME]');
    $allowed_values = $field_storage->getSetting('allowed_values');
    
    // Do your things here.
    ...
    
    // Revert to the original language.
    $language_manager->setConfigOverrideLanguage($current_language); 
    
Production build 0.71.5 2024