2

I need to programmatically set the translated values for several fields on a node. For that I would like to set their values in the fields field_storage_config. Adapted from another question's answer (Manage config translations programmatically) I tried the following, but without success:

$origLang = \Drupal::languageManager()->getConfigOverrideLanguage();
$lang = \Drupal::languageManager()->getLanguage($currentTranslationLanguageId);
$untranslatedStorage = \Drupal::entityTypeManager()->getStorage('field_storage_config')->load("node.$tagsBaseFieldId");
$untranslatedValues = $untranslatedStorage->getSetting('allowed_values');
\Drupal::languageManager()->setConfigOverrideLanguage($lang);
$translatedStorage = \Drupal::entityTypeManager()->getStorage('field_storage_config')->load("node.$tagsBaseFieldId");

I expected to get the field_storage_config of the override language that I set in the languageManager (different from the base language), but if I dump $untranslatedStorage and $translatedStorage after these commands, they are the same. $untranslatedStorage and $translatedStorage are the same object. How is this possible and how can I programmatically set the translations for my field(s). It is a text list field (the one that translates to a selectbox in the UI).

4

How is this possible and how can I programmatically set the translations for my field(s)?

You can load language overrides with ConfigurableLanguageManager::getLanguageConfigOverride, set values and save:

\Drupal::languageManager()->getLanguageConfigOverride('fr', 'field.storage.node.field_example')
  ->set($key, $value)
  ->save();
  • This works, finally. Thanks a lot! – plocks Jul 2 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.