From 66b5cb9b8c6efdec3b0ed05506c1eab6e442f4a0 Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Wed, 4 Feb 2015 10:25:06 +0000 Subject: [PATCH] =?UTF-8?q?Issue=20#2153937=20by=20G=C3=A1bor=20Hojtsy,=20?= =?UTF-8?q?pcambra:=20Default=20language=20setting=20is=20hard=20to=20find?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../block/src/Tests/BlockLanguageTest.php | 4 +- .../src/Tests/ConfigTranslationUiTest.php | 4 +- core/modules/language/language.module | 38 ++----------------- .../language/src/LanguageListBuilder.php | 35 ++++++++++++++++- .../src/Tests/LanguageConfigurationTest.php | 10 ++--- .../language/src/Tests/LanguageListTest.php | 18 ++++----- .../src/Tests/LanguagePathMonolingualTest.php | 2 +- .../locale/src/Tests/LocaleContentTest.php | 2 +- .../src/Tests/NodeTypeInitialLanguageTest.php | 4 +- .../search/src/Tests/SearchLanguageTest.php | 6 +-- 10 files changed, 62 insertions(+), 61 deletions(-) diff --git a/core/modules/block/src/Tests/BlockLanguageTest.php b/core/modules/block/src/Tests/BlockLanguageTest.php index df5e6939fb42..d8e9f1889eef 100644 --- a/core/modules/block/src/Tests/BlockLanguageTest.php +++ b/core/modules/block/src/Tests/BlockLanguageTest.php @@ -34,7 +34,7 @@ protected function setUp() { parent::setUp(); // Create a new user, allow him to manage the blocks and the languages. - $this->adminUser = $this->drupalCreateUser(array('administer blocks', 'administer languages', 'administer site configuration')); + $this->adminUser = $this->drupalCreateUser(array('administer blocks', 'administer languages')); $this->drupalLogin($this->adminUser); // Add predefined language. @@ -68,7 +68,7 @@ public function testLanguageBlockVisibility() { $edit = array( 'site_default_language' => 'fr', ); - $this->drupalPostForm('admin/config/regional/settings', $edit, t('Save configuration')); + $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration')); // Check that a page has a block. $this->drupalGet('en'); diff --git a/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php b/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php index 2224ba4f1b0e..8671c3cae3d1 100644 --- a/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php +++ b/core/modules/config_translation/src/Tests/ConfigTranslationUiTest.php @@ -668,8 +668,8 @@ public function testSingleLanguageUI() { $edit = array( 'site_default_language' => 'ta', ); - $this->drupalPostForm('admin/config/regional/settings', $edit, t('Save configuration')); - $this->assertRaw(t('The configuration options have been saved.')); + $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration')); + $this->assertRaw(t('Configuration saved.')); // Delete English language $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete')); diff --git a/core/modules/language/language.module b/core/modules/language/language.module index 01f3d0b35c94..d5c238bc5799 100644 --- a/core/modules/language/language.module +++ b/core/modules/language/language.module @@ -55,13 +55,15 @@ function language_help($route_name, RouteMatchInterface $route_match) { return $output; case 'entity.configurable_language.collection': - return '<p>' . t('Reorder the added languages to set their order in the language switcher block and, when editing content, in the list of selectable languages. This ordering does not impact <a href="@detection">detection and selection</a>.', array('@detection' => \Drupal::url('language.negotiation'))) . '</p>'; + $output = '<p>' . t('Reorder the added languages to set their order in the language switcher block and, when editing content, in the list of selectable languages. This ordering does not impact <a href="@detection">detection and selection</a>.', array('@detection' => \Drupal::url('language.negotiation'))) . '</p>'; + $output .= '<p>' . t('The site default language can also be set. It is not recommended to change the default language on a working site. <a href="@language-detection">Configure the Selected language</a> setting on the detection and selection page to change the fallback language for language selection.', array('@language-detection' => \Drupal::url('language.negotiation'))) . '</p>'; + return $output; case 'language.add': return '<p>' . t('Add a language to be supported by your site. If your desired language is not available, pick <em>Custom language...</em> at the end and provide a language code and other details manually.') . '</p>'; case 'language.negotiation': - $output = '<p>' . t('Define how to decide which language is used to display page elements (primarily text provided by modules, such as field labels and help text). This decision is made by evaluating a series of detection methods for languages; the first detection method that gets a result will determine which language is used for that type of text. Be aware that some language negotiation methods are unreliable under certain conditions, such as browser detection when page-caching is enabled and a user is not currently logged in. Define the order of evaluation of language detection methods on this page. The default language can be changed at the <a href="!region-settings">Regional settings</a> page.', array('!region-settings' => \Drupal::url('system.regional_settings'))) . '</p>'; + $output = '<p>' . t('Define how to decide which language is used to display page elements (primarily text provided by modules, such as field labels and help text). This decision is made by evaluating a series of detection methods for languages; the first detection method that gets a result will determine which language is used for that type of text. Be aware that some language negotiation methods are unreliable under certain conditions, such as browser detection when page-caching is enabled and a user is not currently logged in. Define the order of evaluation of language detection methods on this page. The default language can be changed in the <a href="!admin-change-language">list of languages</a>.', array('!admin-change-language' => \Drupal::url('entity.configurable_language.collection'))) . '</p>'; return $output; case 'language.negotiation_session': @@ -448,29 +450,6 @@ function language_get_browser_drupal_langcode_mappings() { return $config->get(); } -/** - * Implements hook_form_FORM_ID_alter for system_regional_settings(). - * - * @see language_system_regional_settings_form_submit() - */ -function language_form_system_regional_settings_alter(&$form, FormStateInterface $form_state) { - $languages = \Drupal::languageManager()->getLanguages(); - $default = \Drupal::languageManager()->getDefaultLanguage(); - foreach ($languages as $key => $language) { - $language_options[$key] = $language->getName(); - } - $form['locale']['site_default_language'] = array( - '#type' => 'select', - '#title' => t('Default language'), - '#default_value' => $default->getId(), - '#options' => $language_options, - '#description' => t('It is not recommended to change the default language on a working site. <a href="@language-detection">Configure the Selected language</a> setting on the detection and selection page to change the fallback language for language selection.', array('@language-detection' => \Drupal::url('language.negotiation'))), - '#weight' => -1, - ); - // Add submit handler to save default language. - $form['#submit'][] = 'language_system_regional_settings_form_submit'; -} - /** * Implements hook_form_alter(). */ @@ -490,15 +469,6 @@ function language_form_alter(&$form, FormStateInterface $form_state) { } } -/** - * Form submission handler for system_regional_settings(). - * - * @see language_form_system_regional_settings_alter() - */ -function language_system_regional_settings_form_submit($form, FormStateInterface $form_state) { - \Drupal::configFactory()->getEditable('system.site')->set('langcode', $form_state->getValue('site_default_language'))->save(); -} - /** * Implements hook_field_info_alter(). */ diff --git a/core/modules/language/src/LanguageListBuilder.php b/core/modules/language/src/LanguageListBuilder.php index 5cf54b96727a..24fd280151c0 100644 --- a/core/modules/language/src/LanguageListBuilder.php +++ b/core/modules/language/src/LanguageListBuilder.php @@ -13,6 +13,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageManagerInterface; +use Drupal\Core\Render\Element; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -83,8 +84,11 @@ public function getFormId() { * {@inheritdoc} */ public function buildHeader() { - $header['label'] = t('Name'); - return $header + parent::buildHeader(); + $header = array( + 'label' => t('Name'), + 'default' => t('Default'), + ) + parent::buildHeader(); + return $header; } /** @@ -92,6 +96,14 @@ public function buildHeader() { */ public function buildRow(EntityInterface $entity) { $row['label'] = $this->getLabel($entity); + $row['default'] = array( + '#type' => 'radio', + '#parents' => array('site_default_language'), + '#title' => t('Set @title as default', array('@title' => $entity->label())), + '#title_display' => 'invisible', + '#return_value' => $entity->id(), + '#id' => 'edit-site-default-language-' . $entity->id(), + ); return $row + parent::buildRow($entity); } @@ -100,6 +112,15 @@ public function buildRow(EntityInterface $entity) { */ public function buildForm(array $form, FormStateInterface $form_state) { $form = parent::buildForm($form, $form_state); + + // Mark the right language as default in the form. + $default = \Drupal::languageManager()->getDefaultLanguage(); + foreach (Element::children($form[$this->entitiesKey]) as $key) { + if ($key == $default->getId()) { + $form[$this->entitiesKey][$key]['default']['#default_value'] = $default->getId(); + } + } + $form[$this->entitiesKey]['#languages'] = $this->entities; $form['actions']['submit']['#value'] = t('Save configuration'); return $form; @@ -111,12 +132,22 @@ public function buildForm(array $form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) { parent::submitForm($form, $form_state); + // Save the default language. + foreach ($form_state->getValue($this->entitiesKey) as $id => $value) { + if (isset($this->entities[$id]) && ($id == $form_state->getValue('site_default_language'))) { + \Drupal::configFactory()->getEditable('system.site')->set('langcode', $form_state->getValue('site_default_language'))->save(); + } + } + $this->languageManager->reset(); if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) { $this->languageManager->updateLockedLanguageWeights(); } drupal_set_message(t('Configuration saved.')); + // Force the redirection to the page with the language we have just + // selected as default. + $form_state->setRedirect('entity.configurable_language.collection', array(), array('language' => $this->entities[$form_state->getValue('site_default_language')])); } } diff --git a/core/modules/language/src/Tests/LanguageConfigurationTest.php b/core/modules/language/src/Tests/LanguageConfigurationTest.php index 9a43437c84b6..a5efb2fd37c4 100644 --- a/core/modules/language/src/Tests/LanguageConfigurationTest.php +++ b/core/modules/language/src/Tests/LanguageConfigurationTest.php @@ -30,7 +30,7 @@ class LanguageConfigurationTest extends WebTestBase { function testLanguageConfiguration() { // User to add and remove language. - $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'administer site configuration')); + $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages')); $this->drupalLogin($admin_user); // Check if the Default English language has no path prefix. @@ -56,8 +56,8 @@ function testLanguageConfiguration() { $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French has a path prefix.'); // Check if we can change the default language. - $this->drupalGet('admin/config/regional/settings'); - $this->assertOptionSelected('edit-site-default-language', 'en', 'English is the default language.'); + $this->drupalGet('admin/config/regional/language'); + $this->assertFieldChecked('edit-site-default-language-en', 'English is the default language.'); // Change the default language. $edit = array( @@ -65,8 +65,8 @@ function testLanguageConfiguration() { ); $this->drupalPostForm(NULL, $edit, t('Save configuration')); $this->rebuildContainer(); - $this->assertOptionSelected('edit-site-default-language', 'fr', 'Default language updated.'); - $this->assertUrl(\Drupal::url('system.regional_settings', [], ['absolute' => TRUE, 'langcode' => 'fr']), [], 'Correct page redirection.'); + $this->assertFieldChecked('edit-site-default-language-fr', 'Default language updated.'); + $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'langcode' => 'fr']), [], 'Correct page redirection.'); // Check if a valid language prefix is added after changing the default // language. diff --git a/core/modules/language/src/Tests/LanguageListTest.php b/core/modules/language/src/Tests/LanguageListTest.php index 88e227f3022a..8ec846c7108c 100644 --- a/core/modules/language/src/Tests/LanguageListTest.php +++ b/core/modules/language/src/Tests/LanguageListTest.php @@ -32,7 +32,7 @@ class LanguageListTest extends WebTestBase { function testLanguageList() { // User to add and remove language. - $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages', 'administer site configuration')); + $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages')); $this->drupalLogin($admin_user); // Add predefined language. @@ -61,17 +61,17 @@ function testLanguageList() { $english = \Drupal::service('language_manager')->getLanguage('en'); // Check if we can change the default language. - $path = 'admin/config/regional/settings'; + $path = 'admin/config/regional/language'; $this->drupalGet($path); - $this->assertOptionSelected('edit-site-default-language', 'en', 'English is the default language.'); + $this->assertFieldChecked('edit-site-default-language-en', 'English is the default language.'); // Change the default language. $edit = array( 'site_default_language' => $langcode, ); $this->drupalPostForm(NULL, $edit, t('Save configuration')); $this->rebuildContainer(); - $this->assertNoOptionSelected('edit-site-default-language', 'en', 'Default language updated.'); - $this->assertUrl(\Drupal::url('system.regional_settings', [], ['absolute' => TRUE, 'language' => $language])); + $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.'); + $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language])); // Ensure we can't delete the default language. $this->drupalGet('admin/config/regional/language/delete/' . $langcode); @@ -146,17 +146,17 @@ function testLanguageList() { $this->assertText($name, 'Name found.'); // Check if we can change the default language. - $path = 'admin/config/regional/settings'; + $path = 'admin/config/regional/language'; $this->drupalGet($path); - $this->assertOptionSelected('edit-site-default-language', 'en', 'English is the default language.'); + $this->assertFieldChecked('edit-site-default-language-en', 'English is the default language.'); // Change the default language. $edit = array( 'site_default_language' => $langcode, ); $this->drupalPostForm(NULL, $edit, t('Save configuration')); $this->rebuildContainer(); - $this->assertNoOptionSelected('edit-site-default-language', 'en', 'Default language updated.'); - $this->assertUrl(\Drupal::url('system.regional_settings', [], ['absolute' => TRUE, 'language' => $language])); + $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.'); + $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language])); $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete')); // We need raw here because %language and %langcode will add HTML. diff --git a/core/modules/language/src/Tests/LanguagePathMonolingualTest.php b/core/modules/language/src/Tests/LanguagePathMonolingualTest.php index c574f9177bef..d3aa8141f408 100644 --- a/core/modules/language/src/Tests/LanguagePathMonolingualTest.php +++ b/core/modules/language/src/Tests/LanguagePathMonolingualTest.php @@ -39,7 +39,7 @@ protected function setUp() { $edit = array( 'site_default_language' => 'fr', ); - $this->drupalPostForm('admin/config/regional/settings', $edit, t('Save configuration')); + $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration')); // Delete English. $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete')); diff --git a/core/modules/locale/src/Tests/LocaleContentTest.php b/core/modules/locale/src/Tests/LocaleContentTest.php index 2b66904fec40..93a5e77d535c 100644 --- a/core/modules/locale/src/Tests/LocaleContentTest.php +++ b/core/modules/locale/src/Tests/LocaleContentTest.php @@ -47,7 +47,7 @@ public function testMachineNameLTR() { $edit = array( 'site_default_language' => 'ar', ); - $this->drupalPostForm('admin/config/regional/settings', $edit, t('Save configuration')); + $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration')); // Verify that the machine name field is still LTR for a new content type. $this->drupalGet('admin/structure/types/add'); diff --git a/core/modules/node/src/Tests/NodeTypeInitialLanguageTest.php b/core/modules/node/src/Tests/NodeTypeInitialLanguageTest.php index 06793b4c5eb2..bca545f6ee70 100644 --- a/core/modules/node/src/Tests/NodeTypeInitialLanguageTest.php +++ b/core/modules/node/src/Tests/NodeTypeInitialLanguageTest.php @@ -26,7 +26,7 @@ class NodeTypeInitialLanguageTest extends NodeTestBase { protected function setUp() { parent::setUp(); - $web_user = $this->drupalCreateUser(array('bypass node access', 'administer content types', 'administer node fields', 'administer node form display', 'administer node display', 'administer languages', 'administer site configuration')); + $web_user = $this->drupalCreateUser(array('bypass node access', 'administer content types', 'administer node fields', 'administer node form display', 'administer node display', 'administer languages')); $this->drupalLogin($web_user); } @@ -57,7 +57,7 @@ function testNodeTypeInitialLanguageDefaults() { $edit = array( 'site_default_language' => 'hu', ); - $this->drupalPostForm('admin/config/regional/settings', $edit, t('Save configuration')); + $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration')); // Tests the initial language after changing the site default language. // First unhide the language selector. diff --git a/core/modules/search/src/Tests/SearchLanguageTest.php b/core/modules/search/src/Tests/SearchLanguageTest.php index 6376dd02562b..b6f8d2b8ce3a 100644 --- a/core/modules/search/src/Tests/SearchLanguageTest.php +++ b/core/modules/search/src/Tests/SearchLanguageTest.php @@ -128,14 +128,14 @@ function testLanguages() { $this->assertNoLink('Third node en', 'Search results does not contain third English node'); // Change the default language and delete English. - $path = 'admin/config/regional/settings'; + $path = 'admin/config/regional/language'; $this->drupalGet($path); - $this->assertOptionSelected('edit-site-default-language', 'en', 'Default language updated.'); + $this->assertFieldChecked('edit-site-default-language-en', 'Default language updated.'); $edit = array( 'site_default_language' => 'fr', ); $this->drupalPostForm($path, $edit, t('Save configuration')); - $this->assertNoOptionSelected('edit-site-default-language', 'en', 'Default language updated.'); + $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.'); $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete')); } } -- GitLab