0

At some point we will be enabling translations on all entities in Drupal 8. However, we are not ready yet so in the meantime we would like to set all of the entities (content types, taxonomy terms, paragraphs, menus, blocks) to be defaulted to English language. We want to do this because we do not want any entity to have a language code of undefined (or und).

I am unable to find the setting that will allow us to default all entities to just English.

How can we set the default for all entities as English?

0

You probably want to write this as an update hook, something like this should work:

function MY_MODULE_update_N() {
  foreach (\Drupal::entityTypeManager()->getDefinitions() as $entityType) {
    $entities = \Drupal::entityTypeManager()->getStorage($entityType)->loadMultiple();
    foreach ($entities as $entity) {
      if ($entity->langcode->value != 'en') {
        $entity->set('langcode', 'en');
        $entity->save();
      }
    }
  }
}

This loops through all entity types and loads all entities for the given entity type, then it updates the language if it is not set to english.

You might want to create a batch process if you have many entities to update.

For more info about how to do that, look here.

  • getting all the entities using \Drupal::entityTypeManager()->getDefinitions() did the trick and it also reduced the amount of update hooks i needed to write. – usernameabc Sep 26 at 20:04

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.