diff --git a/core/modules/language/config/language.types.yml b/core/modules/language/config/language.types.yml index 2b77d89..41dc882 100644 --- a/core/modules/language/config/language.types.yml +++ b/core/modules/language/config/language.types.yml @@ -4,3 +4,14 @@ all: - language_url configurable: - language_interface +settings: + language_content: + enabled: + language-interface: 0 + language_url: + enabled: + language-url: 0 + language-url-fallback: 1 + language_interface: + enabled: + language-url: 0 diff --git a/core/modules/language/config/schema/language.schema.yml b/core/modules/language/config/schema/language.schema.yml index ab74403..730ce62 100644 --- a/core/modules/language/config/schema/language.schema.yml +++ b/core/modules/language/config/schema/language.schema.yml @@ -1,5 +1,22 @@ # Schema for the configuration files of the Language module. +language_type_setting: + type: mapping + label: 'Language type setting' + mapping: + enabled: + type: sequence + label: 'Enabled methods' + sequence: + - type: integer + label: Weight + method_weights: + type: sequence + label: 'Method weights' + sequence: + - type: integer + label: Weight + language.types: type: mapping label: 'Language types' @@ -16,6 +33,12 @@ language.types: sequence: - type: string label: 'Language type' + settings: + type: sequence + label: 'Language type settings' + sequence: + - type: language_type_setting + label: 'Language type setting' language.negotiation: type: mapping diff --git a/core/modules/language/language.install b/core/modules/language/language.install index 3738831..a22fac8 100644 --- a/core/modules/language/language.install +++ b/core/modules/language/language.install @@ -5,29 +5,6 @@ * 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(). */ @@ -35,12 +12,6 @@ function language_uninstall() { // Clear variables. variable_del('language_default'); - // 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 // functions will not expect languages to be present. drupal_language_initialize(); diff --git a/core/modules/language/lib/Drupal/language/ConfigurableLanguageManager.php b/core/modules/language/lib/Drupal/language/ConfigurableLanguageManager.php index 49c7ea4..1e2615f 100644 --- a/core/modules/language/lib/Drupal/language/ConfigurableLanguageManager.php +++ b/core/modules/language/lib/Drupal/language/ConfigurableLanguageManager.php @@ -174,8 +174,15 @@ public function getDefinedLanguageTypesInfo() { /** * Stores language types configuration. */ - public function saveLanguageTypesConfiguration(array $config) { - $this->configFactory->get('language.types')->setData($config)->save(); + public function saveLanguageTypesConfiguration(array $values) { + $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(); } /** diff --git a/core/modules/language/lib/Drupal/language/Form/NegotiationConfigureForm.php b/core/modules/language/lib/Drupal/language/Form/NegotiationConfigureForm.php index c5b2a66..29525e9 100644 --- a/core/modules/language/lib/Drupal/language/Form/NegotiationConfigureForm.php +++ b/core/modules/language/lib/Drupal/language/Form/NegotiationConfigureForm.php @@ -150,8 +150,7 @@ public function submitForm(array &$form, array &$form_state) { } $method_weights_type[$type] = $method_weights; - // @todo convert this to config. - variable_set("language_negotiation_methods_weight_$type", $method_weights_input); + $this->config('language.types')->set('settings.' . $type . '.method_weights', $method_weights_input)->save(); } // Update non-configurable language types and the related language @@ -213,8 +212,8 @@ protected function configureFormTable(array &$form, $type) { } $negotiation_info = $form['#language_negotiation_info']; - $enabled_methods = variable_get("language_negotiation_$type", array()); - $methods_weight = variable_get("language_negotiation_methods_weight_$type", array()); + $enabled_methods = $this->config('language.types')->get('settings.' . $type . '.enabled') ?: array(); + $methods_weight = $this->config('language.types')->get('settings.' . $type . '.method_weights') ?: array(); // Add missing data to the methods lists. foreach ($negotiation_info as $method_id => $method) { diff --git a/core/modules/language/lib/Drupal/language/LanguageNegotiator.php b/core/modules/language/lib/Drupal/language/LanguageNegotiator.php index f2a74f6..e090ea4 100644 --- a/core/modules/language/lib/Drupal/language/LanguageNegotiator.php +++ b/core/modules/language/lib/Drupal/language/LanguageNegotiator.php @@ -36,7 +36,7 @@ class LanguageNegotiator implements LanguageNegotiatorInterface { /** * The configuration factory. * - * @var \Drupal\Core\Config\config + * @var \Drupal\Core\Config\ConfigFactory */ protected $configFactory; @@ -138,7 +138,7 @@ public function initializeType($type) { if ($this->currentUser && $this->request) { // Execute the language negotiation methods in the order they were set up // and return the first valid language found. - foreach ($this->getConfiguration($type) as $method_id => $info) { + foreach ($this->getEnabledMethods($type) as $method_id => $info) { if (!isset($this->negotiatedLanguages[$method_id])) { $this->negotiatedLanguages[$method_id] = $this->negotiateLanguage($type, $method_id); } @@ -166,13 +166,16 @@ public function initializeType($type) { } /** - * {@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) { - // @todo convert to CMI https://drupal.org/node/1827038 and - // https://drupal.org/node/2102477 - drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES, FALSE); - return variable_get("language_negotiation_$type", array()); + protected function getEnabledMethods($type) { + return $this->configFactory->get('language.types')->get('settings.' . $type . '.enabled') ?: array(); } /** @@ -218,8 +221,8 @@ protected function negotiateLanguage($type, $method_id) { public function getNegotiationMethods($type = NULL) { $definitions = $this->negotiatorManager->getDefinitions(); if (isset($type)) { - $config = $this->getConfiguration($type); - $definitions = array_intersect_key($definitions, $config); + $enabled_methods = $this->getEnabledMethods($type); + $definitions = array_intersect_key($definitions, $enabled_methods); } return $definitions; } @@ -242,8 +245,8 @@ public function getNegotiationMethodInstance($method_id) { * {@inheritdoc} */ public function getPrimaryNegotiationMethod($type) { - $config = $this->getConfiguration($type); - return empty($config) ? LanguageNegotiatorInterface::METHOD_ID : key($config); + $enabled_methods = $this->getEnabledMethods($type); + return empty($enabled_methods) ? LanguageNegotiatorInterface::METHOD_ID : key($enabled_methods); } /** @@ -254,8 +257,8 @@ public function isNegotiationMethodEnabled($method_id, $type = NULL) { $language_types = !empty($type) ? array($type) : $this->languageManager->getLanguageTypes(); foreach ($language_types as $type) { - $config = $this->getConfiguration($type); - if (isset($config[$method_id])) { + $enabled_methods = $this->getEnabledMethods($type); + if (isset($enabled_methods['$method_id'])) { $enabled = TRUE; break; } @@ -267,13 +270,13 @@ public function isNegotiationMethodEnabled($method_id, $type = NULL) { /** * {@inheritdoc} */ - function saveConfiguration($type, $method_weights) { + function saveConfiguration($type, $enabled_methods) { $definitions = $this->getNegotiationMethods(); $default_types = $this->languageManager->getLanguageTypes(); // Order the language negotiation method list by weight. - asort($method_weights); - foreach ($method_weights as $method_id => $weight) { + asort($enabled_methods); + foreach ($enabled_methods as $method_id => $weight) { if (isset($definitions[$method_id])) { $method = $definitions[$method_id]; // If the language negotiation method does not express any preference @@ -281,15 +284,14 @@ function saveConfiguration($type, $method_weights) { $types = array_flip(!empty($method['types']) ? $method['types'] : $default_types); // Check whether the method is defined and has the right type. if (!isset($types[$type])) { - unset($method_weights[$method_id]); + unset($enabled_methods[$method_id]); } } else { - unset($method_weights[$method_id]); + unset($enabled_methods[$method_id]); } } - - variable_set("language_negotiation_$type", $method_weights); + $this->configFactory->get('language.types')->set('settings.' . $type . '.enabled', $enabled_methods)->save(); } /** @@ -303,7 +305,7 @@ function purgeConfiguration() { $this->negotiatorManager->clearCachedDefinitions(); $this->languageManager->reset(); foreach ($this->languageManager->getDefinedLanguageTypesInfo() as $type => $info) { - $this->saveConfiguration($type, $this->getConfiguration($type)); + $this->saveConfiguration($type, $this->getEnabledMethods($type)); } } diff --git a/core/modules/language/lib/Drupal/language/LanguageNegotiatorInterface.php b/core/modules/language/lib/Drupal/language/LanguageNegotiatorInterface.php index 0654fde..d12fd6f 100644 --- a/core/modules/language/lib/Drupal/language/LanguageNegotiatorInterface.php +++ b/core/modules/language/lib/Drupal/language/LanguageNegotiatorInterface.php @@ -196,10 +196,10 @@ public function isNegotiationMethodEnabled($method_id, $type = NULL); * * @param string $type * The language type. - * @param array $method_weights + * @param array $enabled_methods * 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. diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php index 471dab2..b15522c 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php @@ -91,7 +91,7 @@ function testInfoAlterations() { // negotiation settings with the proper flag enabled. \Drupal::state()->set('language_test.language_negotiation_info_alter', TRUE); $this->languageNegotiationUpdate(); - $negotiation = variable_get("language_negotiation_$type", array()); + $negotiation = \Drupal::config('language.types')->get('settings.' . $type . '.enabled') ?: array(); $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.'); @@ -131,7 +131,7 @@ function testInfoAlterations() { // Check that unavailable language negotiation methods are not present in // the negotiation settings. - $negotiation = variable_get("language_negotiation_$type", array()); + $negotiation = \Drupal::config('language.types')->get('settings.' . $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.'); // Check that configuration page presents the correct options and settings. @@ -173,7 +173,7 @@ protected function checkFixedLanguageTypes() { $configurable = $this->languageManager->getLanguageTypes(); foreach ($this->languageManager->getDefinedLanguageTypesInfo() as $type => $info) { if (!in_array($type, $configurable) && isset($info['fixed'])) { - $negotiation = variable_get("language_negotiation_$type", array()); + $negotiation = \Drupal::config('language.types')->get('settings.' . $type . '.enabled') ?: array(); $equal = count($info['fixed']) == count($negotiation); while ($equal && list($id) = each($negotiation)) { list(, $info_id) = each($info['fixed']); diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php index 1a07175..3bae013 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php @@ -238,7 +238,9 @@ function testUILanguageNegotiation() { // Unknown language prefix should return 404. $definitions = \Drupal::languageManager()->getNegotiator()->getNegotiationMethods(); - variable_set('language_negotiation_' . Language::TYPE_INTERFACE, array_flip(array_keys($definitions))); + \Drupal::config('language.types') + ->set('settings.' . Language::TYPE_INTERFACE . '.enabled', array_flip(array_keys($definitions))) + ->save(); $this->drupalGet("$langcode_unknown/admin/config", array(), $http_header_browser_fallback); $this->assertResponse(404, "Unknown language path prefix should return 404");