Issue #2350933 by rodrigoaguilera, alexpott, Gábor Hojtsy, YesCT: Languages don't get weights by default, reordered when displayed translated

8.0.x
webchick 2015-02-18 14:39:27 -08:00
parent e6cd1fa5b7
commit 1f2d857984
4 changed files with 46 additions and 2 deletions

View File

@ -131,7 +131,7 @@ class ConfigurableLanguage extends ConfigEntityBase implements ConfigurableLangu
$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 @@ class ConfigurableLanguage extends ConfigEntityBase implements ConfigurableLangu
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

View File

@ -159,6 +159,11 @@ class LanguageAddForm extends LanguageFormBase {
$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);
}
}

View File

@ -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 @@ class LanguageConfigurationTest extends WebTestBase {
$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.');
}
/**

View File

@ -35,6 +35,10 @@ class LanguageListTest extends WebTestBase {
$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 @@ class LanguageListTest extends WebTestBase {
$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);