Allow default_langcode field value to be changed

Created on 2 March 2015, over 9 years ago
Updated 4 April 2024, 3 months 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

11.0 🔥

Component
Entity  →

Last updated 1 day ago

  • Maintained by
  • 🇬🇧United Kingdom @catch
  • 🇨🇭Switzerland @Berdir
  • 🇩🇪Germany @hchonov
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 12 months 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 12 months 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
    3 months ago
    Total: 232s
    #137623
Production build 0.69.0 2024