Issue #2102477 by alexpott: Convert remainder of language negotiation settings to configuration system.

8.0.x
Nathaniel Catchpole 2014-01-27 11:41:02 +00:00
parent b8ef75c5f4
commit 3764fe395c
9 changed files with 78 additions and 63 deletions

View File

@ -4,3 +4,14 @@ all:
- language_url - language_url
configurable: configurable:
- language_interface - language_interface
negotiation:
language_content:
enabled:
language-interface: 0
language_url:
enabled:
language-url: 0
language-url-fallback: 1
language_interface:
enabled:
language-url: 0

View File

@ -1,5 +1,22 @@
# Schema for the configuration files of the Language module. # Schema for the configuration files of the Language module.
language_type_negotiation:
type: mapping
label: 'Language negotiation per type setting'
mapping:
enabled:
type: sequence
label: 'Enabled negotiators'
sequence:
- type: integer
label: Weight
method_weights:
type: sequence
label: 'Negotiator weights'
sequence:
- type: integer
label: Weight
language.types: language.types:
type: mapping type: mapping
label: 'Language types' label: 'Language types'
@ -16,6 +33,12 @@ language.types:
sequence: sequence:
- type: string - type: string
label: 'Language type' label: 'Language type'
negotiation:
type: sequence
label: 'Language negotiation per type settings'
sequence:
- type: language_type_negotiation
label: 'Language negotiation per type setting'
language.negotiation: language.negotiation:
type: mapping type: mapping

View File

@ -5,39 +5,10 @@
* Install, update and uninstall functions for the language module. * Install, update and uninstall functions for the language module.
*/ */
use Drupal\Core\Language\Language;
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
/**
* Implements hook_install().
*
* Enable URL language negotiation by default in order to have a basic working
* system on multilingual sites without needing any preliminary configuration.
*/
function language_install() {
$language_manager = \Drupal::languageManager();
if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
$negotiator = \Drupal::service('language_negotiator');
$types = $language_manager->getLanguageTypes();
$negotiator->updateConfiguration($types);
// Enable URL language detection for each configurable language type.
foreach ($types as $type) {
$negotiator->saveConfiguration($type, array(LanguageNegotiationUrl::METHOD_ID => 0));
}
}
}
/** /**
* Implements hook_uninstall(). * Implements hook_uninstall().
*/ */
function language_uninstall() { function language_uninstall() {
// Clear variables.
foreach (\Drupal::languageManager()->getDefinedLanguageTypes() as $type) {
variable_del("language_negotiation_$type");
variable_del("language_negotiation_methods_weight_$type");
}
// Re-initialize the language system so successive calls to t() and other // Re-initialize the language system so successive calls to t() and other
// functions will not expect languages to be present. // functions will not expect languages to be present.
drupal_language_initialize(); drupal_language_initialize();

View File

@ -169,8 +169,15 @@ class ConfigurableLanguageManager extends LanguageManager implements Configurabl
/** /**
* Stores language types configuration. * Stores language types configuration.
*/ */
public function saveLanguageTypesConfiguration(array $config) { public function saveLanguageTypesConfiguration(array $values) {
$this->configFactory->get('language.types')->setData($config)->save(); $config = $this->configFactory->get('language.types');
if (isset($values['configurable'])) {
$config->set('configurable', $values['configurable']);
}
if (isset($values['all'])) {
$config->set('all', $values['all']);
}
$config->save();
} }
/** /**

View File

@ -150,8 +150,7 @@ class NegotiationConfigureForm extends FormBase {
} }
$method_weights_type[$type] = $method_weights; $method_weights_type[$type] = $method_weights;
// @todo convert this to config. $this->config('language.types')->set('negotiation.' . $type . '.method_weights', $method_weights_input)->save();
variable_set("language_negotiation_methods_weight_$type", $method_weights_input);
} }
// Update non-configurable language types and the related language // Update non-configurable language types and the related language
@ -213,8 +212,8 @@ class NegotiationConfigureForm extends FormBase {
} }
$negotiation_info = $form['#language_negotiation_info']; $negotiation_info = $form['#language_negotiation_info'];
$enabled_methods = variable_get("language_negotiation_$type", array()); $enabled_methods = $this->config('language.types')->get('negotiation.' . $type . '.enabled') ?: array();
$methods_weight = variable_get("language_negotiation_methods_weight_$type", array()); $methods_weight = $this->config('language.types')->get('negotiation.' . $type . '.method_weights') ?: array();
// Add missing data to the methods lists. // Add missing data to the methods lists.
foreach ($negotiation_info as $method_id => $method) { foreach ($negotiation_info as $method_id => $method) {

View File

@ -36,7 +36,7 @@ class LanguageNegotiator implements LanguageNegotiatorInterface {
/** /**
* The configuration factory. * The configuration factory.
* *
* @var \Drupal\Core\Config\config * @var \Drupal\Core\Config\ConfigFactory
*/ */
protected $configFactory; protected $configFactory;
@ -138,7 +138,7 @@ class LanguageNegotiator implements LanguageNegotiatorInterface {
if ($this->currentUser && $this->request) { if ($this->currentUser && $this->request) {
// Execute the language negotiation methods in the order they were set up // Execute the language negotiation methods in the order they were set up
// and return the first valid language found. // and return the first valid language found.
foreach ($this->getConfiguration($type) as $method_id => $info) { foreach ($this->getEnabledNegotiators($type) as $method_id => $info) {
if (!isset($this->negotiatedLanguages[$method_id])) { if (!isset($this->negotiatedLanguages[$method_id])) {
$this->negotiatedLanguages[$method_id] = $this->negotiateLanguage($type, $method_id); $this->negotiatedLanguages[$method_id] = $this->negotiateLanguage($type, $method_id);
} }
@ -166,13 +166,16 @@ class LanguageNegotiator implements LanguageNegotiatorInterface {
} }
/** /**
* {@inheritdoc} * Gets enabled detection methods for the provided language type.
*
* @param string $type
* The language type.
*
* @return array
* An array of enabled detection methods for the provided language type.
*/ */
protected function getConfiguration($type) { protected function getEnabledNegotiators($type) {
// @todo convert to CMI https://drupal.org/node/1827038 and return $this->configFactory->get('language.types')->get('negotiation.' . $type . '.enabled') ?: array();
// https://drupal.org/node/2102477
drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE);
return variable_get("language_negotiation_$type", array());
} }
/** /**
@ -218,8 +221,8 @@ class LanguageNegotiator implements LanguageNegotiatorInterface {
public function getNegotiationMethods($type = NULL) { public function getNegotiationMethods($type = NULL) {
$definitions = $this->negotiatorManager->getDefinitions(); $definitions = $this->negotiatorManager->getDefinitions();
if (isset($type)) { if (isset($type)) {
$config = $this->getConfiguration($type); $enabled_methods = $this->getEnabledNegotiators($type);
$definitions = array_intersect_key($definitions, $config); $definitions = array_intersect_key($definitions, $enabled_methods);
} }
return $definitions; return $definitions;
} }
@ -242,8 +245,8 @@ class LanguageNegotiator implements LanguageNegotiatorInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPrimaryNegotiationMethod($type) { public function getPrimaryNegotiationMethod($type) {
$config = $this->getConfiguration($type); $enabled_methods = $this->getEnabledNegotiators($type);
return empty($config) ? LanguageNegotiatorInterface::METHOD_ID : key($config); return empty($enabled_methods) ? LanguageNegotiatorInterface::METHOD_ID : key($enabled_methods);
} }
/** /**
@ -254,8 +257,8 @@ class LanguageNegotiator implements LanguageNegotiatorInterface {
$language_types = !empty($type) ? array($type) : $this->languageManager->getLanguageTypes(); $language_types = !empty($type) ? array($type) : $this->languageManager->getLanguageTypes();
foreach ($language_types as $type) { foreach ($language_types as $type) {
$config = $this->getConfiguration($type); $enabled_methods = $this->getEnabledNegotiators($type);
if (isset($config[$method_id])) { if (isset($enabled_methods[$method_id])) {
$enabled = TRUE; $enabled = TRUE;
break; break;
} }
@ -267,13 +270,13 @@ class LanguageNegotiator implements LanguageNegotiatorInterface {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
function saveConfiguration($type, $method_weights) { function saveConfiguration($type, $enabled_methods) {
$definitions = $this->getNegotiationMethods(); $definitions = $this->getNegotiationMethods();
$default_types = $this->languageManager->getLanguageTypes(); $default_types = $this->languageManager->getLanguageTypes();
// Order the language negotiation method list by weight. // Order the language negotiation method list by weight.
asort($method_weights); asort($enabled_methods);
foreach ($method_weights as $method_id => $weight) { foreach ($enabled_methods as $method_id => $weight) {
if (isset($definitions[$method_id])) { if (isset($definitions[$method_id])) {
$method = $definitions[$method_id]; $method = $definitions[$method_id];
// If the language negotiation method does not express any preference // If the language negotiation method does not express any preference
@ -281,15 +284,14 @@ class LanguageNegotiator implements LanguageNegotiatorInterface {
$types = array_flip(!empty($method['types']) ? $method['types'] : $default_types); $types = array_flip(!empty($method['types']) ? $method['types'] : $default_types);
// Check whether the method is defined and has the right type. // Check whether the method is defined and has the right type.
if (!isset($types[$type])) { if (!isset($types[$type])) {
unset($method_weights[$method_id]); unset($enabled_methods[$method_id]);
} }
} }
else { else {
unset($method_weights[$method_id]); unset($enabled_methods[$method_id]);
} }
} }
$this->configFactory->get('language.types')->set('negotiation.' . $type . '.enabled', $enabled_methods)->save();
variable_set("language_negotiation_$type", $method_weights);
} }
/** /**
@ -303,7 +305,7 @@ class LanguageNegotiator implements LanguageNegotiatorInterface {
$this->negotiatorManager->clearCachedDefinitions(); $this->negotiatorManager->clearCachedDefinitions();
$this->languageManager->reset(); $this->languageManager->reset();
foreach ($this->languageManager->getDefinedLanguageTypesInfo() as $type => $info) { foreach ($this->languageManager->getDefinedLanguageTypesInfo() as $type => $info) {
$this->saveConfiguration($type, $this->getConfiguration($type)); $this->saveConfiguration($type, $this->getEnabledNegotiators($type));
} }
} }

View File

@ -196,10 +196,10 @@ interface LanguageNegotiatorInterface {
* *
* @param string $type * @param string $type
* The language type. * The language type.
* @param array $method_weights * @param array $enabled_methods
* An array of language negotiation method weights keyed by method ID. * An array of language negotiation method weights keyed by method ID.
*/ */
function saveConfiguration($type, $method_weights); function saveConfiguration($type, $enabled_methods);
/** /**
* Resave the configuration to purge missing negotiation methods. * Resave the configuration to purge missing negotiation methods.

View File

@ -91,7 +91,7 @@ class LanguageNegotiationInfoTest extends WebTestBase {
// negotiation settings with the proper flag enabled. // negotiation settings with the proper flag enabled.
\Drupal::state()->set('language_test.language_negotiation_info_alter', TRUE); \Drupal::state()->set('language_test.language_negotiation_info_alter', TRUE);
$this->languageNegotiationUpdate(); $this->languageNegotiationUpdate();
$negotiation = variable_get("language_negotiation_$type", array()); $negotiation = \Drupal::config('language.types')->get('negotiation.' . $type . '.enabled') ?: array();
$this->assertFalse(isset($negotiation[$interface_method_id]), 'Interface language negotiation method removed from the stored settings.'); $this->assertFalse(isset($negotiation[$interface_method_id]), 'Interface language negotiation method removed from the stored settings.');
$this->assertNoFieldByXPath("//input[@name=\"$form_field\"]", NULL, 'Interface language negotiation method unavailable.'); $this->assertNoFieldByXPath("//input[@name=\"$form_field\"]", NULL, 'Interface language negotiation method unavailable.');
@ -131,7 +131,7 @@ class LanguageNegotiationInfoTest extends WebTestBase {
// Check that unavailable language negotiation methods are not present in // Check that unavailable language negotiation methods are not present in
// the negotiation settings. // the negotiation settings.
$negotiation = variable_get("language_negotiation_$type", array()); $negotiation = \Drupal::config('language.types')->get('negotiation.' . $type . '.enabled') ?: array();
$this->assertFalse(isset($negotiation[$test_method_id]), 'The disabled test language negotiation method is not part of the content language negotiation settings.'); $this->assertFalse(isset($negotiation[$test_method_id]), 'The disabled test language negotiation method is not part of the content language negotiation settings.');
// Check that configuration page presents the correct options and settings. // Check that configuration page presents the correct options and settings.
@ -173,7 +173,7 @@ class LanguageNegotiationInfoTest extends WebTestBase {
$configurable = $this->languageManager->getLanguageTypes(); $configurable = $this->languageManager->getLanguageTypes();
foreach ($this->languageManager->getDefinedLanguageTypesInfo() as $type => $info) { foreach ($this->languageManager->getDefinedLanguageTypesInfo() as $type => $info) {
if (!in_array($type, $configurable) && isset($info['fixed'])) { if (!in_array($type, $configurable) && isset($info['fixed'])) {
$negotiation = variable_get("language_negotiation_$type", array()); $negotiation = \Drupal::config('language.types')->get('negotiation.' . $type . '.enabled') ?: array();
$equal = count($info['fixed']) == count($negotiation); $equal = count($info['fixed']) == count($negotiation);
while ($equal && list($id) = each($negotiation)) { while ($equal && list($id) = each($negotiation)) {
list(, $info_id) = each($info['fixed']); list(, $info_id) = each($info['fixed']);

View File

@ -242,7 +242,9 @@ class LanguageUILanguageNegotiationTest extends WebTestBase {
// Unknown language prefix should return 404. // Unknown language prefix should return 404.
$definitions = \Drupal::languageManager()->getNegotiator()->getNegotiationMethods(); $definitions = \Drupal::languageManager()->getNegotiator()->getNegotiationMethods();
variable_set('language_negotiation_' . Language::TYPE_INTERFACE, array_flip(array_keys($definitions))); \Drupal::config('language.types')
->set('negotiation.' . Language::TYPE_INTERFACE . '.enabled', array_flip(array_keys($definitions)))
->save();
$this->drupalGet("$langcode_unknown/admin/config", array(), $http_header_browser_fallback); $this->drupalGet("$langcode_unknown/admin/config", array(), $http_header_browser_fallback);
$this->assertResponse(404, "Unknown language path prefix should return 404"); $this->assertResponse(404, "Unknown language path prefix should return 404");