Commit 801d850a authored by catch's avatar catch

Issue #2337847 by alexpott, YesCT, martin107: The negotiated method_id does...

Issue #2337847 by alexpott, YesCT, martin107: The negotiated method_id does not need to be sorted on the Language or ConfigurableLanguage object.
parent 3660df9a
......@@ -67,16 +67,6 @@ class Language implements LanguageInterface {
*/
public $default = FALSE;
/**
* The language negotiation method used when a language was detected.
*
* The method ID, for example
* \Drupal\language\LanguageNegotiatorInterface::METHOD_ID.
*
* @var string
*/
public $method_id;
/**
* Locked indicates a language used by the system, not an actual language.
*
......
......@@ -80,6 +80,13 @@ class ConfigurableLanguageManager extends LanguageManager implements Configurabl
*/
protected $negotiatedLanguages;
/**
* An array of language negotiation method IDs keyed by language type.
*
* @var array
*/
protected $negotiatedMethods;
/**
* Whether or not the language manager has been initialized.
*
......@@ -209,7 +216,9 @@ public function getCurrentLanguage($type = LanguageInterface::TYPE_INTERFACE) {
if ($this->negotiator && $this->isMultilingual()) {
if (!$this->initializing) {
$this->initializing = TRUE;
$this->negotiatedLanguages[$type] = $this->negotiator->initializeType($type);
$negotiation = $this->negotiator->initializeType($type);
$this->negotiatedLanguages[$type] = reset($negotiation);
$this->negotiatedMethods[$type] = key($negotiation);
$this->initializing = FALSE;
}
// If the current interface language needs to be retrieved during
......@@ -234,6 +243,7 @@ public function reset($type = NULL) {
if (!isset($type)) {
$this->initialized = FALSE;
$this->negotiatedLanguages = array();
$this->negotiatedMethods = array();
$this->languageTypes = NULL;
$this->languageTypesInfo = NULL;
$this->languages = NULL;
......@@ -243,6 +253,7 @@ public function reset($type = NULL) {
}
elseif (isset($this->negotiatedLanguages[$type])) {
unset($this->negotiatedLanguages[$type]);
unset($this->negotiatedMethods[$type]);
}
return $this;
}
......@@ -453,4 +464,13 @@ public function getStandardLanguageListWithoutConfigured() {
return $predefined;
}
/**
* {@inheritdoc}
*/
public function getNegotiatedLanguageMethod($type = LanguageInterface::TYPE_INTERFACE) {
if (isset($this->negotiatedLanguages[$type]) && isset($this->negotiatedMethods[$type])) {
return $this->negotiatedMethods[$type];
}
}
}
......@@ -7,6 +7,7 @@
namespace Drupal\language;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\HttpFoundation\Request;
......@@ -96,4 +97,16 @@ public function getLanguageConfigOverrideStorage($langcode);
*/
public function getStandardLanguageListWithoutConfigured();
/**
* Gets the negotiated language method ID.
*
* @param string $type
* (optional) The language type; e.g., the interface or the content
* language.
*
* @return string
* The negotiated language method ID.
*/
public function getNegotiatedLanguageMethod($type = LanguageInterface::TYPE_INTERFACE);
}
......@@ -110,16 +110,6 @@ class ConfigurableLanguage extends ConfigEntityBase implements ConfigurableLangu
*/
protected $preSaveMultilingual;
/**
* The language negotiation method used when a language was detected.
*
* The method ID, for example
* \Drupal\language\LanguageNegotiatorInterface::METHOD_ID.
*
* @var string
*/
public $method_id;
/**
* Checks if the language entity is the site default language.
*
......
......@@ -62,8 +62,7 @@ public function setCurrentUser(AccountInterface $current_user) {
* {@inheritdoc}
*/
public function persist(LanguageInterface $language) {
// Remember the method ID used to detect the language.
$language->method_id = static::METHOD_ID;
// Default implementation persists nothing.
}
}
......@@ -73,7 +73,7 @@ class LanguageNegotiator implements LanguageNegotiatorInterface {
*
* @var array
*/
protected $negotiatedLanguages;
protected $negotiatedLanguages = array();
/**
* Constructs a new LanguageNegotiator object.
......@@ -152,10 +152,10 @@ public function initializeType($type) {
if (!$language) {
// If no other language was found use the default one.
$language = $this->languageManager->getDefaultLanguage();
$language->method_id = LanguageNegotiatorInterface::METHOD_ID;
$method_id = static::METHOD_ID;
}
return $language;
return array($method_id => $language);
}
/**
......
......@@ -128,8 +128,10 @@ public function setCurrentUser(AccountInterface $current_user);
* @param string $type
* The language type to be initialized.
*
* @return \Drupal\Core\Language\LanguageInterface
* Return either the language of the specified type or the default language.
* @return \Drupal\Core\Language\LanguageInterface[]
* Returns an array containing a single language keyed by the language
* negotiation method ID used to determine the language of the specified
* type. If negotiation is not possible the default language is returned.
*/
public function initializeType($type);
......
......@@ -69,8 +69,6 @@ public function getLangcode(Request $request = NULL) {
* {@inheritdoc}
*/
public function persist(LanguageInterface $language) {
parent::persist($language);
// We need to update the session parameter with the request value only if we
// have an authenticated user.
$langcode = $language->id;
......
......@@ -12,9 +12,9 @@
* Implements hook_page_build().
*/
function language_test_page_build() {
language_test_store_language_negotiation();
if (isset(\Drupal::languageManager()->getCurrentLanguage()->id) && isset(\Drupal::languageManager()->getCurrentLanguage()->method_id)) {
drupal_set_message(t('Language negotiation method: @name', array('@name' => \Drupal::languageManager()->getCurrentLanguage()->method_id)));
if (\Drupal::moduleHandler()->moduleExists('language')) {
language_test_store_language_negotiation();
drupal_set_message(t('Language negotiation method: @name', array('@name' => \Drupal::languageManager()->getNegotiatedLanguageMethod())));
}
}
......@@ -66,13 +66,11 @@ function language_test_language_negotiation_info_alter(array &$negotiation_info)
* Store the last negotiated languages.
*/
function language_test_store_language_negotiation() {
if (\Drupal::moduleHandler()->moduleExists('language')) {
$last = array();
foreach (\Drupal::languageManager()->getDefinedLanguageTypes() as $type) {
$last[$type] = \Drupal::languageManager()->getCurrentLanguage($type)->id;
}
\Drupal::state()->set('language_test.language_negotiation_last', $last);
$last = array();
foreach (\Drupal::languageManager()->getDefinedLanguageTypes() as $type) {
$last[$type] = \Drupal::languageManager()->getCurrentLanguage($type)->id;
}
\Drupal::state()->set('language_test.language_negotiation_last', $last);
}
/**
......
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