Commit f3b3f268 authored by catch's avatar catch

Issue #2313157 by Gábor Hojtsy: Fixed Optimize admin language detection and make it optional.

parent 381b05e5
...@@ -90,28 +90,34 @@ public function isAnonymous(); ...@@ -90,28 +90,34 @@ public function isAnonymous();
/** /**
* Returns the preferred language code of the account. * Returns the preferred language code of the account.
* *
* @param string $default * @param bool $fallback_to_default
* (optional) Default language code to return if the account * (optional) Whether the return value will fall back to the site default
* has no valid language, defaults to the site default language. * language if the user has no language preference.
* *
* @return string * @return string
* The language code that is preferred by the account. * The language code that is preferred by the account. If the preferred
* language is not set or is a language not configured anymore on the site,
* the site default is returned or an empty string is returned (if
* $fallback_to_default is FALSE).
*/ */
public function getPreferredLangcode($default = NULL); public function getPreferredLangcode($fallback_to_default = TRUE);
/** /**
* Returns the preferred administrative language code of the account. * Returns the preferred administrative language code of the account.
* *
* Defines which language is used on administrative pages. * Defines which language is used on administrative pages.
* *
* @param string $default * @param bool $fallback_to_default
* (optional) Default language code to return if the account * (optional) Whether the return value will fall back to the site default
* has no valid language, defaults to the site default language. * language if the user has no administration language preference.
* *
* @return string * @return string
* The language code that is preferred by the account. * The language code that is preferred by the account for administration
* pages. If the preferred language is not set or is a language not
* configured anymore on the site, the site default is returned or an empty
* string is returned (if $fallback_to_default is FALSE).
*/ */
public function getPreferredAdminLangcode($default = NULL); public function getPreferredAdminLangcode($fallback_to_default = TRUE);
/** /**
* Returns the username of this account. * Returns the username of this account.
......
...@@ -147,15 +147,15 @@ public function isAnonymous() { ...@@ -147,15 +147,15 @@ public function isAnonymous() {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPreferredLangcode($default = NULL) { public function getPreferredLangcode($fallback_to_default = TRUE) {
return $this->getAccount()->getPreferredLangcode($default); return $this->getAccount()->getPreferredLangcode($fallback_to_default);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPreferredAdminLangcode($default = NULL) { public function getPreferredAdminLangcode($fallback_to_default = TRUE) {
return $this->getAccount()->getPreferredAdminLangcode($default); return $this->getAccount()->getPreferredAdminLangcode($fallback_to_default);
} }
/** /**
......
...@@ -182,26 +182,26 @@ public function isAnonymous() { ...@@ -182,26 +182,26 @@ public function isAnonymous() {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
function getPreferredLangcode($default = NULL) { function getPreferredLangcode($fallback_to_default = TRUE) {
$language_list = language_list(); $language_list = language_list();
if (!empty($this->preferred_langcode) && isset($language_list[$this->preferred_langcode])) { if (!empty($this->preferred_langcode) && isset($language_list[$this->preferred_langcode])) {
return $language_list[$this->preferred_langcode]->id; return $language_list[$this->preferred_langcode]->id;
} }
else { else {
return $default ? $default : language_default()->id; return $fallback_to_default ? language_default()->id : '';
} }
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
function getPreferredAdminLangcode($default = NULL) { function getPreferredAdminLangcode($fallback_to_default = TRUE) {
$language_list = language_list(); $language_list = language_list();
if (!empty($this->preferred_admin_langcode) && isset($language_list[$this->preferred_admin_langcode])) { if (!empty($this->preferred_admin_langcode) && isset($language_list[$this->preferred_admin_langcode])) {
return $language_list[$this->preferred_admin_langcode]->id; return $language_list[$this->preferred_admin_langcode]->id;
} }
else { else {
return $default ? $default : language_default()->id; return $fallback_to_default ? language_default()->id : '';
} }
} }
......
...@@ -248,7 +248,7 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -248,7 +248,7 @@ public function form(array $form, FormStateInterface $form_state) {
$user_preferred_langcode = $register ? $language_interface->id : $account->getPreferredLangcode(); $user_preferred_langcode = $register ? $language_interface->id : $account->getPreferredLangcode();
$user_preferred_admin_langcode = $register ? $language_interface->id : $account->getPreferredAdminLangcode(); $user_preferred_admin_langcode = $register ? $language_interface->id : $account->getPreferredAdminLangcode(FALSE);
// Is the user preferred language added? // Is the user preferred language added?
$user_language_added = FALSE; $user_language_added = FALSE;
...@@ -286,6 +286,8 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -286,6 +286,8 @@ public function form(array $form, FormStateInterface $form_state) {
'#languages' => LanguageInterface::STATE_CONFIGURABLE, '#languages' => LanguageInterface::STATE_CONFIGURABLE,
'#default_value' => $user_preferred_admin_langcode, '#default_value' => $user_preferred_admin_langcode,
'#access' => $show_admin_language, '#access' => $show_admin_language,
'#empty_option' => $this->t('- No preference -'),
'#empty_value' => '',
); );
// User entities contain both a langcode property (for identifying the // User entities contain both a langcode property (for identifying the
// language of the entity data) and a preferred_langcode property (see // language of the entity data) and a preferred_langcode property (see
......
...@@ -366,28 +366,28 @@ public function getTimeZone() { ...@@ -366,28 +366,28 @@ public function getTimeZone() {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
function getPreferredLangcode($default = NULL) { function getPreferredLangcode($fallback_to_default = TRUE) {
$language_list = language_list(); $language_list = language_list();
$preferred_langcode = $this->get('preferred_langcode')->value; $preferred_langcode = $this->get('preferred_langcode')->value;
if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) { if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) {
return $language_list[$preferred_langcode]->id; return $language_list[$preferred_langcode]->id;
} }
else { else {
return $default ? $default : language_default()->id; return $fallback_to_default ? language_default()->id : '';
} }
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
function getPreferredAdminLangcode($default = NULL) { function getPreferredAdminLangcode($fallback_to_default = TRUE) {
$language_list = language_list(); $language_list = language_list();
$preferred_langcode = $this->get('preferred_admin_langcode')->value; $preferred_langcode = $this->get('preferred_admin_langcode')->value;
if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) { if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) {
return $language_list[$preferred_langcode]->id; return $language_list[$preferred_langcode]->id;
} }
else { else {
return $default ? $default : language_default()->id; return $fallback_to_default ? language_default()->id : '';
} }
} }
......
...@@ -92,17 +92,12 @@ public static function create(ContainerInterface $container, array $configuratio ...@@ -92,17 +92,12 @@ public static function create(ContainerInterface $container, array $configuratio
public function getLangcode(Request $request = NULL) { public function getLangcode(Request $request = NULL) {
$langcode = NULL; $langcode = NULL;
// User preference (only for authenticated users). // User preference (only for administrators).
if ($this->languageManager && $this->currentUser->isAuthenticated() && $this->isAdminPath($request)) { if ($this->currentUser->hasPermission('access administration pages') && ($preferred_admin_langcode = $this->currentUser->getPreferredAdminLangcode(FALSE)) && $this->isAdminPath($request)) {
$preferred_admin_langcode = $this->currentUser->getPreferredAdminLangcode(); $langcode = $preferred_admin_langcode;
$default_langcode = $this->languageManager->getDefaultLanguage()->id;
$languages = $this->languageManager->getLanguages();
if (!empty($preferred_admin_langcode) && $preferred_admin_langcode != $default_langcode && isset($languages[$preferred_admin_langcode])) {
$langcode = $preferred_admin_langcode;
}
} }
// No language preference from the user or not on an admin path. // Not an admin, no admin language preference or not on an admin path.
return $langcode; return $langcode;
} }
......
...@@ -118,31 +118,37 @@ function testActualNegotiation() { ...@@ -118,31 +118,37 @@ function testActualNegotiation() {
$path = 'user/' . $this->adminUser->id() . '/edit'; $path = 'user/' . $this->adminUser->id() . '/edit';
$this->drupalGet($path); $this->drupalGet($path);
$this->assertText('Language negotiation method: language-default'); $this->assertText('Language negotiation method: language-default');
$this->drupalGet('xx/' . $path);
$this->assertText('Language negotiation method: language-url');
// Set a preferred language code for the user. // Set a preferred language code for the user.
$path = 'user/' . $this->adminUser->id() . '/edit';
$edit = array(); $edit = array();
$edit['preferred_admin_langcode'] = 'xx'; $edit['preferred_admin_langcode'] = 'xx';
$this->drupalPostForm($path, $edit, t('Save')); $this->drupalPostForm($path, $edit, t('Save'));
// Test negotiation with the URL method first. The admin method will only // Test negotiation with the URL method first. The admin method will only
// be used if the URL method did not match. // be used if the URL method did not match.
$path = 'user/' . $this->adminUser->id() . '/edit';
$this->drupalGet($path); $this->drupalGet($path);
$this->assertText('Language negotiation method: language-user-admin'); $this->assertText('Language negotiation method: language-user-admin');
$path = 'xx/user/' . $this->adminUser->id() . '/edit'; $this->drupalGet('xx/' . $path);
$this->drupalGet($path);
$this->assertText('Language negotiation method: language-url'); $this->assertText('Language negotiation method: language-url');
// Test negotiation with the admin language method first. The admin method // Test negotiation with the admin language method first. The admin method
// will be used at all times. // will be used at all times.
$this->setLanguageNegotiation(TRUE); $this->setLanguageNegotiation(TRUE);
$path = 'user/' . $this->adminUser->id() . '/edit';
$this->drupalGet($path); $this->drupalGet($path);
$this->assertText('Language negotiation method: language-user-admin'); $this->assertText('Language negotiation method: language-user-admin');
$path = 'xx/user/' . $this->adminUser->id() . '/edit'; $this->drupalGet('xx/' . $path);
$this->drupalGet($path);
$this->assertText('Language negotiation method: language-user-admin'); $this->assertText('Language negotiation method: language-user-admin');
// Unset the preferred language code for the user.
$edit = array();
$edit['preferred_admin_langcode'] = '';
$this->drupalPostForm($path, $edit, t('Save'));
$this->drupalGet($path);
$this->assertText('Language negotiation method: language-default');
$this->drupalGet('xx/' . $path);
$this->assertText('Language negotiation method: language-url');
} }
/** /**
......
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