Allow default_langcode field value to be changed

Created on 2 March 2015, about 10 years ago
Updated 14 July 2023, almost 2 years ago

Problem/Motivation

In #2431329: Make (content) translation language available as a field definition we introduced a translated default_langcode field, but we prevented it from being altered as it should be considered read-only. We did not use a standard method to implement this as read-only constraints were not working at the time. Moreover until #2137801: Refactor entity storage to load field values before instantiating entity objects is fixed, the field is actually populated so it cannot be read-only. However we should switch to a standard method as soon as possible.

Proposed resolution

TBD

Remaining tasks

  • Figure out a solution
  • Write a patch
  • Review it

User interface changes

None

API changes

None

📌 Task
Status

Active

Version

9.5

Component
Entity 

Last updated 2 days ago

Created by

🇮🇹Italy plach Venezia

Live updates comments and jobs are added and updated live.
  • D8MI

    (Drupal 8 Multilingual Initiative) is the tag used by the multilingual initiative to mark core issues (and some contributed module issues). For versions other than Drupal 8, use the i18n (Internationalization) tag on issues which involve or affect multilingual / multinational support. That is preferred over Translation.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇩🇪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
  • 🇧🇪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
  • 🇧🇪Belgium wim leers Ghent 🇧🇪🇪🇺

    Mea culpa — I thought it was about a default_langcode setting in some Field(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.
  • Pipeline finished with Failed
    about 1 year ago
    Total: 232s
    #137623
  • 🇩🇰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 ...

Production build 0.71.5 2024