- 🇩🇪Germany sascha_meissner Planet earth
In case anyone might find this useful, this code will remove the edit-langcode element from ContentEntityForms if there are already translations created to prevent the error "Can't change the default langauge"
/** * Implements hook_form_alter(). */ function yourmodule_form_alter(&$form, FormStateInterface $form_state, $form_id) { $form_object = $form_state->getFormObject(); if ($form_object instanceof ContentEntityFormInterface) { // Prevent switching languages on already translated content. // @see \Drupal\content_translation\ContentTranslationHandler::entityFormAlter. $entity = $form_object->getEntity(); if ($entity->isTranslatable() && count($entity->getTranslationLanguages()) > 1) { // Translations exist, prevent language switching. $langcode_key = $entity->getEntityType()->getKey('langcode'); $form[$langcode_key]['widget']['#access'] = FALSE; } } }
- Status changed to Postponed
almost 2 years ago 12:12pm 14 July 2023 - 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
But additionally we need constraints that ensure that there is always a valid default language.
— @mkalkbrenner in #3, >8 years ago.
This is now within reach: ✨ Add a `langcode` data type to config schema Fixed is RTBC, and then we'll still need to do the work here to actually run validation constraints 😊
- 🇨🇭Switzerland berdir Switzerland
@Wim: This is about content entities and the boolean default_langcode flag. (ensuring that exactly one translation has this flag). That has nothing to do with langcode validation in config entities.
- Status changed to Active
almost 2 years ago 12:36pm 14 July 2023 - 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
Mea culpa — I thought it was about a
default_langcode
setting in someField(Storage)Config
. 🙈That being said … exactly the same validation approach will be usable here AFAICT? 😊 That'd be:
constraints: NotNull: [] Choice: callback: 'Drupal\Core\TypedData\Plugin\DataType\LanguageReference::getAllValidLangcodes'
That's also why I didn't dig very deep into this issue, because that alone landing in core in another issue would be sufficient to make this issue more actionable?
- 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺
✨ Add a `langcode` data type to config schema Fixed landed. Per #43, AFAICT that should still help this issue! 😊
- First commit to issue fork.
- Merge request !7331#2443991 Allow default_langcode field value to be changed → (Open) created by Taran2L
- 🇩🇰Denmark ressa Copenhagen
It's a bit of an edge case, but I'll share it here, since this issue pops up when searching for changing default language.
I have originally started my web site in the standard English language, but now want to change to Danish as the default language, because I want to translate block titles and other user interface elements, with a base in Danish. What makes this case special, is that all content is migrated into Drupal from JSON-files, so there is no existing content to change language for, only configuration.
I updated and renamed these configuration files, to change from English to Danish:
sed -i "s|langcode: en|langcode: da|g" *.yml sed -i "s|en: ''|da: ''|g" language.negotiation.yml mv language.entity.en.yml language.entity.da.yml sed -i "s|id: en|id: da|g" language.entity.da.yml sed -i "s|label: English|label: Dansk|g" language.entity.da.yml
I then installed a fresh instance of Drupal in Danish, importing the config:
drush site:install minimal --config-dir=../assets/config --locale=da -y
I installed the language modules, to allow adding English language, as an additional language:
drush install content_translation locale config_translation
Before finding this method, I tried to create Danish language, set it as default, and delete English. But in the exported config files, there were still many lingering
langcode: en
strings in the config files (maybe ~50?), so I thought it better to switch all instances manually. It seems to work well, so far ...