Skip to content
Snippets Groups Projects
Commit 66b5cb9b authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2153937 by Gábor Hojtsy, pcambra: Default language setting is hard to find

parent 8e199103
No related branches found
No related tags found
No related merge requests found
Showing with 62 additions and 61 deletions
...@@ -34,7 +34,7 @@ protected function setUp() { ...@@ -34,7 +34,7 @@ protected function setUp() {
parent::setUp(); parent::setUp();
// Create a new user, allow him to manage the blocks and the languages. // 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); $this->drupalLogin($this->adminUser);
// Add predefined language. // Add predefined language.
...@@ -68,7 +68,7 @@ public function testLanguageBlockVisibility() { ...@@ -68,7 +68,7 @@ public function testLanguageBlockVisibility() {
$edit = array( $edit = array(
'site_default_language' => 'fr', '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. // Check that a page has a block.
$this->drupalGet('en'); $this->drupalGet('en');
......
...@@ -668,8 +668,8 @@ public function testSingleLanguageUI() { ...@@ -668,8 +668,8 @@ public function testSingleLanguageUI() {
$edit = array( $edit = array(
'site_default_language' => 'ta', 'site_default_language' => 'ta',
); );
$this->drupalPostForm('admin/config/regional/settings', $edit, t('Save configuration')); $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
$this->assertRaw(t('The configuration options have been saved.')); $this->assertRaw(t('Configuration saved.'));
// Delete English language // Delete English language
$this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete')); $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete'));
......
...@@ -55,13 +55,15 @@ function language_help($route_name, RouteMatchInterface $route_match) { ...@@ -55,13 +55,15 @@ function language_help($route_name, RouteMatchInterface $route_match) {
return $output; return $output;
case 'entity.configurable_language.collection': 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': 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>'; 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': 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; return $output;
case 'language.negotiation_session': case 'language.negotiation_session':
...@@ -448,29 +450,6 @@ function language_get_browser_drupal_langcode_mappings() { ...@@ -448,29 +450,6 @@ function language_get_browser_drupal_langcode_mappings() {
return $config->get(); 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(). * Implements hook_form_alter().
*/ */
...@@ -490,15 +469,6 @@ function language_form_alter(&$form, FormStateInterface $form_state) { ...@@ -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(). * Implements hook_field_info_alter().
*/ */
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\Element;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
/** /**
...@@ -83,8 +84,11 @@ public function getFormId() { ...@@ -83,8 +84,11 @@ public function getFormId() {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildHeader() { public function buildHeader() {
$header['label'] = t('Name'); $header = array(
return $header + parent::buildHeader(); 'label' => t('Name'),
'default' => t('Default'),
) + parent::buildHeader();
return $header;
} }
/** /**
...@@ -92,6 +96,14 @@ public function buildHeader() { ...@@ -92,6 +96,14 @@ public function buildHeader() {
*/ */
public function buildRow(EntityInterface $entity) { public function buildRow(EntityInterface $entity) {
$row['label'] = $this->getLabel($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); return $row + parent::buildRow($entity);
} }
...@@ -100,6 +112,15 @@ public function buildRow(EntityInterface $entity) { ...@@ -100,6 +112,15 @@ public function buildRow(EntityInterface $entity) {
*/ */
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $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[$this->entitiesKey]['#languages'] = $this->entities;
$form['actions']['submit']['#value'] = t('Save configuration'); $form['actions']['submit']['#value'] = t('Save configuration');
return $form; return $form;
...@@ -111,12 +132,22 @@ public function buildForm(array $form, FormStateInterface $form_state) { ...@@ -111,12 +132,22 @@ public function buildForm(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $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(); $this->languageManager->reset();
if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) { if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) {
$this->languageManager->updateLockedLanguageWeights(); $this->languageManager->updateLockedLanguageWeights();
} }
drupal_set_message(t('Configuration saved.')); 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')]));
} }
} }
...@@ -30,7 +30,7 @@ class LanguageConfigurationTest extends WebTestBase { ...@@ -30,7 +30,7 @@ class LanguageConfigurationTest extends WebTestBase {
function testLanguageConfiguration() { function testLanguageConfiguration() {
// User to add and remove language. // 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); $this->drupalLogin($admin_user);
// Check if the Default English language has no path prefix. // Check if the Default English language has no path prefix.
...@@ -56,8 +56,8 @@ function testLanguageConfiguration() { ...@@ -56,8 +56,8 @@ function testLanguageConfiguration() {
$this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French has a path prefix.'); $this->assertFieldByXPath('//input[@name="prefix[fr]"]', 'fr', 'French has a path prefix.');
// Check if we can change the default language. // Check if we can change the default language.
$this->drupalGet('admin/config/regional/settings'); $this->drupalGet('admin/config/regional/language');
$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. // Change the default language.
$edit = array( $edit = array(
...@@ -65,8 +65,8 @@ function testLanguageConfiguration() { ...@@ -65,8 +65,8 @@ function testLanguageConfiguration() {
); );
$this->drupalPostForm(NULL, $edit, t('Save configuration')); $this->drupalPostForm(NULL, $edit, t('Save configuration'));
$this->rebuildContainer(); $this->rebuildContainer();
$this->assertOptionSelected('edit-site-default-language', 'fr', 'Default language updated.'); $this->assertFieldChecked('edit-site-default-language-fr', 'Default language updated.');
$this->assertUrl(\Drupal::url('system.regional_settings', [], ['absolute' => TRUE, 'langcode' => 'fr']), [], 'Correct page redirection.'); $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 // Check if a valid language prefix is added after changing the default
// language. // language.
......
...@@ -32,7 +32,7 @@ class LanguageListTest extends WebTestBase { ...@@ -32,7 +32,7 @@ class LanguageListTest extends WebTestBase {
function testLanguageList() { function testLanguageList() {
// User to add and remove language. // 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); $this->drupalLogin($admin_user);
// Add predefined language. // Add predefined language.
...@@ -61,17 +61,17 @@ function testLanguageList() { ...@@ -61,17 +61,17 @@ function testLanguageList() {
$english = \Drupal::service('language_manager')->getLanguage('en'); $english = \Drupal::service('language_manager')->getLanguage('en');
// Check if we can change the default language. // Check if we can change the default language.
$path = 'admin/config/regional/settings'; $path = 'admin/config/regional/language';
$this->drupalGet($path); $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. // Change the default language.
$edit = array( $edit = array(
'site_default_language' => $langcode, 'site_default_language' => $langcode,
); );
$this->drupalPostForm(NULL, $edit, t('Save configuration')); $this->drupalPostForm(NULL, $edit, t('Save configuration'));
$this->rebuildContainer(); $this->rebuildContainer();
$this->assertNoOptionSelected('edit-site-default-language', 'en', 'Default language updated.'); $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.');
$this->assertUrl(\Drupal::url('system.regional_settings', [], ['absolute' => TRUE, 'language' => $language])); $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language]));
// Ensure we can't delete the default language. // Ensure we can't delete the default language.
$this->drupalGet('admin/config/regional/language/delete/' . $langcode); $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
...@@ -146,17 +146,17 @@ function testLanguageList() { ...@@ -146,17 +146,17 @@ function testLanguageList() {
$this->assertText($name, 'Name found.'); $this->assertText($name, 'Name found.');
// Check if we can change the default language. // Check if we can change the default language.
$path = 'admin/config/regional/settings'; $path = 'admin/config/regional/language';
$this->drupalGet($path); $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. // Change the default language.
$edit = array( $edit = array(
'site_default_language' => $langcode, 'site_default_language' => $langcode,
); );
$this->drupalPostForm(NULL, $edit, t('Save configuration')); $this->drupalPostForm(NULL, $edit, t('Save configuration'));
$this->rebuildContainer(); $this->rebuildContainer();
$this->assertNoOptionSelected('edit-site-default-language', 'en', 'Default language updated.'); $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.');
$this->assertUrl(\Drupal::url('system.regional_settings', [], ['absolute' => TRUE, 'language' => $language])); $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language]));
$this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete')); $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete'));
// We need raw here because %language and %langcode will add HTML. // We need raw here because %language and %langcode will add HTML.
......
...@@ -39,7 +39,7 @@ protected function setUp() { ...@@ -39,7 +39,7 @@ protected function setUp() {
$edit = array( $edit = array(
'site_default_language' => 'fr', '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. // Delete English.
$this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete')); $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete'));
......
...@@ -47,7 +47,7 @@ public function testMachineNameLTR() { ...@@ -47,7 +47,7 @@ public function testMachineNameLTR() {
$edit = array( $edit = array(
'site_default_language' => 'ar', '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. // Verify that the machine name field is still LTR for a new content type.
$this->drupalGet('admin/structure/types/add'); $this->drupalGet('admin/structure/types/add');
......
...@@ -26,7 +26,7 @@ class NodeTypeInitialLanguageTest extends NodeTestBase { ...@@ -26,7 +26,7 @@ class NodeTypeInitialLanguageTest extends NodeTestBase {
protected function setUp() { protected function setUp() {
parent::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); $this->drupalLogin($web_user);
} }
...@@ -57,7 +57,7 @@ function testNodeTypeInitialLanguageDefaults() { ...@@ -57,7 +57,7 @@ function testNodeTypeInitialLanguageDefaults() {
$edit = array( $edit = array(
'site_default_language' => 'hu', '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. // Tests the initial language after changing the site default language.
// First unhide the language selector. // First unhide the language selector.
......
...@@ -128,14 +128,14 @@ function testLanguages() { ...@@ -128,14 +128,14 @@ function testLanguages() {
$this->assertNoLink('Third node en', 'Search results does not contain third English node'); $this->assertNoLink('Third node en', 'Search results does not contain third English node');
// Change the default language and delete English. // Change the default language and delete English.
$path = 'admin/config/regional/settings'; $path = 'admin/config/regional/language';
$this->drupalGet($path); $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( $edit = array(
'site_default_language' => 'fr', 'site_default_language' => 'fr',
); );
$this->drupalPostForm($path, $edit, t('Save configuration')); $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')); $this->drupalPostForm('admin/config/regional/language/delete/en', array(), t('Delete'));
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment