diff --git a/core/modules/hal/src/Tests/NormalizeTest.php b/core/modules/hal/src/Tests/NormalizeTest.php index ee40c25..39678e3 100644 --- a/core/modules/hal/src/Tests/NormalizeTest.php +++ b/core/modules/hal/src/Tests/NormalizeTest.php @@ -79,13 +79,13 @@ public function testNormalize() { ), $relation_uri => array( array( - 'href' => $this->getEntityUri($target_entity_de), - 'lang' => 'de', - ), - array( 'href' => $this->getEntityUri($target_entity_en), 'lang' => 'en', ), + array( + 'href' => $this->getEntityUri($target_entity_de), + 'lang' => 'de', + ), ), ), '_embedded' => array( @@ -93,7 +93,7 @@ public function testNormalize() { array( '_links' => array( 'self' => array( - 'href' => $this->getEntityUri($target_entity_de), + 'href' => $this->getEntityUri($target_entity_en), ), 'type' => array( 'href' => $type_uri, @@ -101,15 +101,15 @@ public function testNormalize() { ), 'uuid' => array( array( - 'value' => $target_entity_de->uuid(), + 'value' => $target_entity_en->uuid(), ), ), - 'lang' => 'de', + 'lang' => 'en', ), array( '_links' => array( 'self' => array( - 'href' => $this->getEntityUri($target_entity_en), + 'href' => $this->getEntityUri($target_entity_de), ), 'type' => array( 'href' => $type_uri, @@ -117,10 +117,10 @@ public function testNormalize() { ), 'uuid' => array( array( - 'value' => $target_entity_en->uuid(), + 'value' => $target_entity_de->uuid(), ), ), - 'lang' => 'en', + 'lang' => 'de', ), ), ), @@ -136,13 +136,13 @@ public function testNormalize() { ), 'name' => array( array( - 'value' => $values['name'], - 'lang' => 'de', - ), - array( 'value' => $translation_values['name'], 'lang' => 'en', ), + array( + 'value' => $values['name'], + 'lang' => 'de', + ), ), 'field_test_text' => array( array( diff --git a/core/modules/language/src/ConfigurableLanguageManager.php b/core/modules/language/src/ConfigurableLanguageManager.php index a4ca11b..98c5ff0 100644 --- a/core/modules/language/src/ConfigurableLanguageManager.php +++ b/core/modules/language/src/ConfigurableLanguageManager.php @@ -353,10 +353,11 @@ public function updateLockedLanguageWeights() { if (reset($locked_languages)->getWeight() <= $max_weight) { foreach ($locked_languages as $language) { // Update system languages weight. - $max_weight++; - ConfigurableLanguage::load($language->getId()) - ->setWeight($max_weight) - ->save(); + if ($language = ConfigurableLanguage::load($language->getId())) { + $max_weight++; + $language->setWeight($max_weight) + ->save(); + } } } } diff --git a/core/modules/language/src/Entity/ConfigurableLanguage.php b/core/modules/language/src/Entity/ConfigurableLanguage.php index a754654..ecc1e1b 100644 --- a/core/modules/language/src/Entity/ConfigurableLanguage.php +++ b/core/modules/language/src/Entity/ConfigurableLanguage.php @@ -121,6 +121,20 @@ public function preSave(EntityStorageInterface $storage) { // For the uncommon case of custom languages the label should be given in // English. $this->langcode = 'en'; + // When adding a new language the weight shouldn't be zero to avoid a + // reordering of the languages list when their names change i.e. interface + // translation. + // Fetch all configurable languages ordered by weight. + $languages = \Drupal::languageManager()->getLanguages($this::STATE_CONFIGURABLE); + // Retrieve the last. + $last_language = end($languages); + // When adding the first Configurable language leave the weight by default. + // This happens when you are installing language module. + if ($this->isNew() && $last_language->getId() != $this->getId()) { + // The newly created language will have the weight of the heaviest + // language +1. + $this->setWeight($last_language->getWeight() + 1); + } } /** diff --git a/core/modules/language/src/Tests/LanguageListTest.php b/core/modules/language/src/Tests/LanguageListTest.php index 52f6e4c..d1affab 100644 --- a/core/modules/language/src/Tests/LanguageListTest.php +++ b/core/modules/language/src/Tests/LanguageListTest.php @@ -35,6 +35,10 @@ function testLanguageList() { $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'administer site configuration')); $this->drupalLogin($admin_user); + // Get the weight of the last language. + $languages = \Drupal::service('language_manager')->getLanguages(); + $last_language_weight = end($languages)->getWeight(); + // Add predefined language. $edit = array( 'predefined_langcode' => 'fr', @@ -43,6 +47,14 @@ function testLanguageList() { $this->assertText('French', 'Language added successfully.'); $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE])); + // Get the weight of the last language and check that the weight is one unit + // heavier and that is French. + $this->rebuildContainer(); + $languages = \Drupal::service('language_manager')->getLanguages(); + $last_language = end($languages); + $this->AssertEqual($last_language->getWeight(), $last_language_weight + 1); + $this->AssertEqual($last_language->getId(), $edit['predefined_langcode']); + // Add custom language. $langcode = 'xx'; $name = $this->randomMachineName(16); diff --git a/core/modules/system/src/Tests/Entity/EntityTranslationTest.php b/core/modules/system/src/Tests/Entity/EntityTranslationTest.php index 6289c03..d91a136 100644 --- a/core/modules/system/src/Tests/Entity/EntityTranslationTest.php +++ b/core/modules/system/src/Tests/Entity/EntityTranslationTest.php @@ -504,7 +504,8 @@ protected function doTestLanguageFallback($entity_type) { $langcode_key = $this->entityManager->getDefinition($entity_type)->getKey('langcode'); $languages = $this->languageManager->getLanguages(); $language = ConfigurableLanguage::load($languages[$langcode]->getId()); - $language->set('weight', 1); + // Make $language the last. + $language->set('weight', 4); $language->save(); $this->languageManager->reset();