Commit 1f2d8579 authored by webchick's avatar webchick

Issue #2350933 by rodrigoaguilera, alexpott, Gábor Hojtsy, YesCT: Languages...

Issue #2350933 by rodrigoaguilera, alexpott, Gábor Hojtsy, YesCT: Languages don't get weights by default, reordered when displayed translated
parent e6cd1fa5
......@@ -131,7 +131,7 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
$language_manager = \Drupal::languageManager();
$language_manager->reset();
if (!$this->isLocked() && $language_manager instanceof ConfigurableLanguageManagerInterface) {
if (!$this->isLocked() && $language_manager instanceof ConfigurableLanguageManagerInterface && !$this->isSyncing()) {
$language_manager->updateLockedLanguageWeights();
}
......@@ -173,7 +173,8 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti
parent::postDelete($storage, $entities);
$language_manager = \Drupal::languageManager();
$language_manager->reset();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
$entity = reset($entities);
if ($language_manager instanceof ConfigurableLanguageManagerInterface && !$entity->isUninstalling() && !$entity->isSyncing()) {
$language_manager->updateLockedLanguageWeights();
}
// If after deleting this language the site will become monolingual, we need
......
......@@ -159,6 +159,11 @@ protected function copyFormValuesToEntity(EntityInterface $entity, array $form,
$entity->set('id', $langcode);
$entity->set('label', $label);
$entity->set('direction', $direction);
// There is no weight on the edit form. Fetch all configurable languages
// ordered by weight and set the new language to be placed after them.
$languages = \Drupal::languageManager()->getLanguages(ConfigurableLanguage::STATE_CONFIGURABLE);
$last_language = end($languages);
$entity->setWeight($last_language->getWeight() + 1);
}
}
......@@ -8,6 +8,7 @@
namespace Drupal\language\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
/**
......@@ -28,6 +29,9 @@ class LanguageConfigurationTest extends WebTestBase {
* Functional tests for adding, editing and deleting languages.
*/
function testLanguageConfiguration() {
// Ensure the after installing the language module the weight of the English
// language is still 0.
$this->assertEqual(ConfigurableLanguage::load('en')->getWeight(), 0, 'The English language has a weight of 0.');
// User to add and remove language.
$admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
......@@ -106,12 +110,34 @@ function testLanguageConfiguration() {
$this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete'));
$this->rebuildContainer();
$this->assertRaw(t('The %language (%langcode) language has been removed.', array('%language' => 'English', '%langcode' => 'en')));
// Ensure that French language has a weight of 1 after being created through
// the UI.
$french = ConfigurableLanguage::load('fr');
$this->assertEqual($french->getWeight(), 1, 'The French language has a weight of 1.');
// Ensure that French language can now have a weight of 0.
$french->setWeight(0)->save();
$this->assertEqual($french->getWeight(), 0, 'The French language has a weight of 0.');
// Ensure that new languages created through the API get a weight of 0.
$afrikaans = ConfigurableLanguage::createFromLangcode('af');
$afrikaans->save();
$this->assertEqual($afrikaans->getWeight(), 0, 'The Afrikaans language has a weight of 0.');
// Ensure that a new language can be created with any weight.
$arabic = ConfigurableLanguage::createFromLangcode('ar');
$arabic->setWeight(4)->save();
$this->assertEqual($arabic->getWeight(), 4, 'The Arabic language has a weight of 0.');
$edit = array(
'predefined_langcode' => 'de',
);
$this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
$language = $this->config('language.entity.de')->get();
$this->assertEqual($language['langcode'], 'en');
// Ensure that German language has a weight of 5 after being created through
// the UI.
$french = ConfigurableLanguage::load('de');
$this->assertEqual($french->getWeight(), 5, 'The German language has a weight of 5.');
}
/**
......
......@@ -35,6 +35,10 @@ function testLanguageList() {
$admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
$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 than the last configurable language.
$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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment