diff --git a/core/modules/language/language.module b/core/modules/language/language.module index 1dabc31..3eb5f52 100644 --- a/core/modules/language/language.module +++ b/core/modules/language/language.module @@ -288,7 +288,7 @@ function language_configuration_element_submit(&$form, &$form_state) { } /** - * Saves a language configuration that is attached to a bundle or entity type. + * Saves a language configuration that is attached to an entity type and bundle. * * @param string $entity_type * A string representing the entity type. @@ -304,7 +304,7 @@ function language_save_default_configuration($entity_type, $bundle, $values = a } /** - * Returns the language configuration stored for an entity type (and bundle). + * Returns the language configuration stored for an entity type and bundle. * * @param string $entity_type * A string representing the entity type. @@ -325,6 +325,18 @@ function language_get_default_configuration($entity_type, $bundle) { } /** + * Clears the default language configuration for an entity type and bundle. + * + * @param string $entity_type + * A string representing the entity type. + * @param string $bundle + * A string representing the bundle. + */ +function language_clear_default_configuration($entity_type, $bundle) { + config('language.settings')->clear(language_get_default_configuration_settings_path($entity_type, $bundle))->save(); +} + +/** * Returns the root name of the variables used to store the configuration. * * Based on the entity type (and if needed, bundle), the variables used to store @@ -385,7 +397,7 @@ function language_get_default_langcode($entity_type, $bundle) { } // If we did not found a default value so far, invoke all other modules that // may provide a special default language. - $default_value = module_invoke_all('language_default_langcode', $entity_type, $bundle); + $default_value = module_invoke_all('language_default_langcode', $entity_type, $bundle, $configuration); $default_value = array_shift($default_value); if ($default_value) { return $default_value; diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageConfigurationElementTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageConfigurationElementTest.php index 6ad3786..c9f4102 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageConfigurationElementTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageConfigurationElementTest.php @@ -8,6 +8,7 @@ namespace Drupal\language\Tests; use Drupal\simpletest\WebTestBase; +use Drupal\Core\Language\Language; /** * Functional tests for language configuration's effect on negotiation setup. @@ -38,6 +39,9 @@ class LanguageConfigurationElementTest extends WebTestBase { // Check that the settings have been saved. $this->assertEqual($lang_conf['language'], 'current_interface'); $this->assertTrue($lang_conf['language_hidden']); + $this->drupalGet('language-tests/language_configuration_element'); + $this->assertOptionSelected('edit-lang-configuration-language', 'current_interface'); + $this->assertFieldChecked('edit-lang-configuration-language-hidden'); // Reload the page and save again. $this->drupalGet('language-tests/language_configuration_element'); @@ -48,8 +52,53 @@ class LanguageConfigurationElementTest extends WebTestBase { // Check that the settings have been saved. $this->assertEqual($lang_conf['language'], 'authors_default'); $this->assertFalse($lang_conf['language_hidden']); + $this->drupalGet('language-tests/language_configuration_element'); + $this->assertOptionSelected('edit-lang-configuration-language', 'authors_default'); + $this->assertNoFieldChecked('edit-lang-configuration-language-hidden'); + } + + /** + * Tests that the language_get_default_langcode() returns the correct values. + */ + public function testDefaultLangcode() { + // Add some custom languages. + foreach (array('aa', 'bb', 'cc') as $language_code) { + $language = new Language(array( + 'langcode' => $language_code, + 'name' => $this->randomName(), + )); + language_save($language); + } + // Fixed language. + language_save_default_configuration('custom_type', 'custom_bundle', array('language' => 'bb', 'language_hidden' => FALSE)); + $langcode = language_get_default_langcode('custom_type', 'custom_bundle'); + $this->assertEqual($langcode, 'bb'); + + // Current interface. + language_save_default_configuration('custom_type', 'custom_bundle', array('language' => 'current_interface', 'language_hidden' => FALSE)); + $langcode = language_get_default_langcode('custom_type', 'custom_bundle'); + $language_interface = language(LANGUAGE_TYPE_INTERFACE); + $this->assertEqual($langcode, $language_interface->langcode); + + // Site's default. + $old_default = language_default(); + $old_default->default = FALSE; + language_save($old_default); + $new_default = language_load('cc'); + $new_default->default = TRUE; + language_save($new_default); + language_save_default_configuration('custom_type', 'custom_bundle', array('language' => 'site_default', 'language_hidden' => FALSE)); + $langcode = language_get_default_langcode('custom_type', 'custom_bundle'); + $this->assertEqual($langcode, 'cc'); + // @todo: The language_get_default_langcode() uses the global $user to + // return the preferred language, but the global $user is not the logged + // in user from the simpletest browser... - // @todo: check also the interface and write more tests. + // Some custom language code, hook_language_default_langcode() is + // implemented in the language_elements_test module. + language_save_default_configuration('custom_type', 'custom_bundle', array('language' => 'custom_code', 'language_hidden' => FALSE)); + $langcode = language_get_default_langcode('custom_type', 'custom_bundle'); + $this->assertEqual($langcode, 'custom_value'); } } diff --git a/core/modules/language/tests/language_elements_test/language_elements_test.module b/core/modules/language/tests/language_elements_test/language_elements_test.module index 5e43165..2b80796 100644 --- a/core/modules/language/tests/language_elements_test/language_elements_test.module +++ b/core/modules/language/tests/language_elements_test/language_elements_test.module @@ -38,3 +38,12 @@ function language_elements_test_configuration_element() { $form['#submit'][] = 'language_configuration_element_submit'; return $form; } + +/** + * Implements hook_language_default_langcode(). + */ +function language_elements_test_language_default_langcode($entity_type, $bundle, $configuration) { + if ($entity_type == 'custom_type' && $bundle == 'custom_bundle' && $configuration['language'] == 'custom_code') { + return 'custom_value'; + } +} diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermLanguageTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermLanguageTest.php index 4e6878b..c8e2222 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermLanguageTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermLanguageTest.php @@ -33,22 +33,18 @@ class TermLanguageTest extends TaxonomyTestBase { // Create a vocabulary to which the terms will be assigned. $this->vocabulary = $this->createVocabulary(); + + // Add some custom languages. + foreach (array('aa', 'bb', 'cc') as $language_code) { + $language = new Language(array( + 'langcode' => $language_code, + 'name' => $this->randomName(), + )); + language_save($language); + } } function testTermLanguage() { - // Add first some custom languages. - $language = new Language(array( - 'langcode' => 'aa', - 'name' => $this->randomName(), - )); - language_save($language); - - $language = new Language(array( - 'langcode' => 'bb', - 'name' => $this->randomName(), - )); - language_save($language); - // Configure the vocabulary to not hide the language selector. $edit = array( 'default_language[language_hidden]' => FALSE, @@ -81,4 +77,43 @@ class TermLanguageTest extends TaxonomyTestBase { $this->drupalGet('taxonomy/term/' . $term->tid . '/edit'); $this->assertOptionSelected('edit-langcode', $edit['langcode'], t('The term language was correctly selected.')); } + + function testDefaultTermLanguage() { + // Configure the vocabulary to not hide the language selector, and make the + // default language of the terms fixed. + $edit = array( + 'default_language[language]' => 'bb', + 'default_language[language_hidden]' => FALSE, + ); + $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/edit', $edit, t('Save')); + $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add'); + $this->assertOptionSelected('edit-langcode', 'bb'); + + // Make the default language of the terms to be the current interface. + $edit = array( + 'default_language[language]' => 'current_interface', + 'default_language[language_hidden]' => FALSE, + ); + $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/edit', $edit, t('Save')); + $this->drupalGet('aa/admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add'); + $this->assertOptionSelected('edit-langcode', 'aa'); + $this->drupalGet('bb/admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add'); + $this->assertOptionSelected('edit-langcode', 'bb'); + + // Change the default language of the site and check if the default terms + // language is still correctly selected. + $old_default = language_default(); + $old_default->default = FALSE; + language_save($old_default); + $new_default = language_load('cc'); + $new_default->default = TRUE; + language_save($new_default); + $edit = array( + 'default_language[language]' => 'site_default', + 'default_language[language_hidden]' => FALSE, + ); + $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/edit', $edit, t('Save')); + $this->drupalGet('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/add'); + $this->assertOptionSelected('edit-langcode', 'cc'); + } } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyLanguageTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyLanguageTest.php index f4368b7..47b174d 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyLanguageTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyLanguageTest.php @@ -30,10 +30,8 @@ class VocabularyLanguageTest extends TaxonomyTestBase { // Create an administrative user. $this->admin_user = $this->drupalCreateUser(array('administer taxonomy')); $this->drupalLogin($this->admin_user); - } - function testVocabularyLanguage() { - // Add first some custom languages. + // Add some custom languages. $language = new Language(array( 'langcode' => 'aa', 'name' => $this->randomName(), @@ -45,7 +43,9 @@ class VocabularyLanguageTest extends TaxonomyTestBase { 'name' => $this->randomName(), )); language_save($language); + } + function testVocabularyLanguage() { $this->drupalGet('admin/structure/taxonomy/add'); // Check that we have the language selector available. $this->assertField('edit-langcode', t('The language selector field was found on the page')); @@ -70,4 +70,62 @@ class VocabularyLanguageTest extends TaxonomyTestBase { $this->drupalGet('admin/structure/taxonomy/' . $machine_name . '/edit'); $this->assertOptionSelected('edit-langcode', $edit['langcode'], t('The vocabulary language was correctly selected.')); } + + function testVocabularyDefaultLanguageForTerms() { + // Add a new vocabulary and check that the default language settings are for + // the terms are saved. + $edit = array( + 'name' => $this->randomName(), + 'machine_name' => drupal_strtolower($this->randomName()), + 'default_language[language]' => 'bb', + 'default_language[language_hidden]' => FALSE, + ); + $machine_name = $edit['machine_name']; + $this->drupalPost('admin/structure/taxonomy/add', $edit, t('Save')); + // Check that the vocabulary was actually created. + $this->drupalGet('admin/structure/taxonomy/' . $edit['machine_name'] . '/edit'); + $this->assertResponse(200, 'The vocabulary has been created.'); + + // Check that the language settings were saved. + $language_settings = language_get_default_configuration('vocabulary', $edit['machine_name']); + $this->assertEqual($language_settings['language'], 'bb'); + $this->assertEqual($language_settings['language_hidden'], FALSE); + + // Check that the correct options are selected in the interface. + $this->assertOptionSelected('edit-default-language-language', 'bb', 'The correct default language for the terms of this vocabulary is selected.'); + $this->assertNoFieldChecked('edit-default-language-language-hidden', 'Hide language selection option is not checked.'); + + // Edit the vocabulary and check that the new settings are updated. + $edit = array( + 'default_language[language]' => 'aa', + 'default_language[language_hidden]' => TRUE, + ); + $this->drupalPost('admin/structure/taxonomy/' . $machine_name . '/edit', $edit, t('Save')); + + // And check again the settings and also the interface. + $language_settings = language_get_default_configuration('vocabulary', $machine_name); + $this->assertEqual($language_settings['language'], 'aa'); + $this->assertEqual($language_settings['language_hidden'], TRUE); + + $this->drupalGet('admin/structure/taxonomy/' . $machine_name . '/edit'); + $this->assertOptionSelected('edit-default-language-language', 'aa', 'The correct default language for the terms of this vocabulary is selected.'); + $this->assertFieldChecked('edit-default-language-language-hidden', 'Hide language selection option is not checked.'); + + // Check that, if the machine name of the vocabulary is changed, then the + // settings are applied on the new machine name. + $edit = array( + 'machine_name' => $machine_name . '_new', + 'default_language[language]' => 'authors_default', + 'default_language[language_hidden]' => TRUE, + ); + $new_machine_name = $edit['machine_name']; + $this->drupalPost('admin/structure/taxonomy/' . $machine_name . '/edit', $edit, t('Save')); + // Check that the the old settings are empty. + $old_settings = config('language.settings')->get(language_get_default_configuration_settings_path('vocabulary', $machine_name)); + $this->assertNull($old_settings, 'The old vocabulary settings were deleted.'); + // Check that we have the new settings. + $new_settings = language_get_default_configuration('vocabulary', $new_machine_name); + $this->assertEqual($new_settings['language'], 'authors_default'); + $this->assertEqual($new_settings['language_hidden'], TRUE); + } } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyFormController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyFormController.php index 410a75a..0e6cbc2 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyFormController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/VocabularyFormController.php @@ -124,6 +124,12 @@ class VocabularyFormController extends EntityFormController { * */ public function languageConfigurationSubmit(array &$form, array &$form_state) { + $vocabulary = $this->getEntity($form_state); + // Delete the old language settings for the vocabulary, if the machine name + // is changed. + if ($vocabulary && isset($vocabulary->machine_name) && $vocabulary->machine_name != $form_state['values']['machine_name']) { + language_clear_default_configuration('vocabulary', $vocabulary->machine_name); + } // Because the bundle is not yet known and moreover, it can be changed for a // vocabulary, we have to also update the language_configuration_elements in // order to have the correct bundle value.