diff --git a/core/includes/language.inc b/core/includes/language.inc index 6b7329e..8d1dc2a 100644 --- a/core/includes/language.inc +++ b/core/includes/language.inc @@ -274,7 +274,7 @@ function language_negotiation_get_switch_links($type, $path) { } /** - * Removes any language negotiation methods that are no longer defined. + * Resave all negotiation configuration to purge missing negotiation methods. */ function language_negotiation_purge() { // Ensure that we are getting the defined language negotiation information. An @@ -283,17 +283,8 @@ function language_negotiation_purge() { // cached information. Drupal::service('plugin.manager.language_negotiation_method')->clearCachedDefinitions(); drupal_static_reset('language_types_info'); - - $negotiation_info = language_negotiation_info(); foreach (language_types_info() as $type => $type_info) { - $weight = 0; - $method_weights = array(); - foreach (variable_get("language_negotiation_$type", array()) as $method_id) { - if (isset($negotiation_info[$method_id])) { - $method_weights[$method_id] = $weight++; - } - } - language_negotiation_set($type, $method_weights); + language_negotiation_set($type, variable_get("language_negotiation_$type", array())); } } @@ -306,13 +297,11 @@ function language_negotiation_purge() { * An array of language negotiation method weights keyed by method ID. */ function language_negotiation_set($type, $method_weights) { - $methods = array(); $negotiation_info = language_negotiation_info(); $default_types = language_types_get_configurable(); // Order the language negotiation method list by weight. asort($method_weights); - foreach ($method_weights as $method_id => $weight) { if (isset($negotiation_info[$method_id])) { $method = $negotiation_info[$method_id]; @@ -320,13 +309,15 @@ function language_negotiation_set($type, $method_weights) { // about types, make it available for any configurable type. $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])) { - $methods[$method_id] = $method_id; + if (!isset($types[$type])) { + unset($method_weights[$method_id]); } } + else { + unset($method_weights[$method_id]); + } } - - variable_set("language_negotiation_$type", $methods); + variable_set("language_negotiation_$type", $method_weights); } /** diff --git a/core/lib/Drupal/Core/Language/LanguageManager.php b/core/lib/Drupal/Core/Language/LanguageManager.php index 371454b..5ca4880 100644 --- a/core/lib/Drupal/Core/Language/LanguageManager.php +++ b/core/lib/Drupal/Core/Language/LanguageManager.php @@ -204,7 +204,7 @@ public function initializeType($type) { */ protected function getNegotiationForType($type) { // @todo convert to CMI https://drupal.org/node/1827038 - return variable_get("language_negotiation_$type", array()); + return array_keys(variable_get("language_negotiation_$type", array())); } /** diff --git a/core/modules/content_translation/content_translation.install b/core/modules/content_translation/content_translation.install index a1ac73a..aae86b7 100644 --- a/core/modules/content_translation/content_translation.install +++ b/core/modules/content_translation/content_translation.install @@ -86,6 +86,8 @@ function content_translation_install() { // Assign a fairly low weight to ensure our implementation of // hook_module_implements_alter() is run among the last ones. module_set_weight('content_translation', 10); + language_negotiation_include(); + language_negotiation_set(Language::TYPE_CONTENT, array(LanguageNegotiationUrl::METHOD_ID => 0)); } /** diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php index 0bcebb0..d638c6b 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php @@ -158,7 +158,7 @@ function testUILanguageNegotiation() { 'http_header' => $http_header_browser_fallback, 'message' => 'SELECTED: UI language is switched based on selected language.', ); - $this->assertNegotation($test); + $this->runTest($test); // An invalid language is selected. \Drupal::config('language.negotiation')->set('selected_langcode', NULL)->save(); @@ -171,7 +171,7 @@ function testUILanguageNegotiation() { 'http_header' => $http_header_browser_fallback, 'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.', ); - $this->assertNegotation($test); + $this->runTest($test); // No selected language is available. \Drupal::config('language.negotiation')->set('selected_langcode', $langcode_unknown)->save(); @@ -184,7 +184,7 @@ function testUILanguageNegotiation() { 'http_header' => $http_header_browser_fallback, 'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.', ); - $this->assertNegotation($test); + $this->runTest($test); $tests = array( // Default, browser preference should have no influence. @@ -235,7 +235,7 @@ function testUILanguageNegotiation() { ); foreach ($tests as $test) { - $this->assertNegotation($test); + $this->runTest($test); } // Unknown language prefix should return 404. @@ -256,7 +256,7 @@ function testUILanguageNegotiation() { 'http_header' => array(), 'message' => 'USER > DEFAULT: no preferred user language setting, the UI language is default', ); - $this->assertNegotation($test); + $this->runTest($test); // Set preferred langcode for user to unknown language. $account = $this->loggedInUser; @@ -271,7 +271,7 @@ function testUILanguageNegotiation() { 'http_header' => array(), 'message' => 'USER > DEFAULT: invalid preferred user language setting, the UI language is default', ); - $this->assertNegotation($test); + $this->runTest($test); // Set preferred langcode for user to non default. $account->preferred_langcode = $langcode; @@ -285,7 +285,7 @@ function testUILanguageNegotiation() { 'http_header' => array(), 'message' => 'USER > DEFAULT: defined prefereed user language setting, the UI language is based on user setting', ); - $this->assertNegotation($test); + $this->runTest($test); // Set preferred admin langcode for user to NULL. $account->preferred_admin_langcode = NULL; @@ -299,7 +299,7 @@ function testUILanguageNegotiation() { 'http_header' => array(), 'message' => 'USER ADMIN > DEFAULT: no preferred user admin language setting, the UI language is default', ); - $this->assertNegotation($test); + $this->runTest($test); // Set preferred admin langcode for user to unknown language. $account->preferred_admin_langcode = $langcode_unknown; @@ -313,7 +313,7 @@ function testUILanguageNegotiation() { 'http_header' => array(), 'message' => 'USER ADMIN > DEFAULT: invalid preferred user admin language setting, the UI language is default', ); - $this->assertNegotation($test); + $this->runTest($test); // Set preferred admin langcode for user to non default. $account->preferred_admin_langcode = $langcode; @@ -327,11 +327,11 @@ function testUILanguageNegotiation() { 'http_header' => array(), 'message' => 'USER ADMIN > DEFAULT: defined prefereed user admin language setting, the UI language is based on user setting', ); - $this->assertNegotation($test); + $this->runTest($test); } - protected function assertNegotation($test) { + protected function runTest($test) { if (!empty($test['language_negotiation'])) { $method_weights = array_flip($test['language_negotiation']); language_negotiation_set(Language::TYPE_INTERFACE, $method_weights); diff --git a/core/modules/locale/locale.install b/core/modules/locale/locale.install index d1a77d7..5f2008f 100644 --- a/core/modules/locale/locale.install +++ b/core/modules/locale/locale.install @@ -501,20 +501,10 @@ function locale_update_8004() { $types = update_variable_get('language_types', NULL); if (!empty($types)) { foreach ($types as $type => $configurable) { - // Rename the negotiation and language switch callback keys. + // Change the structure of the language negotiation configuration. $negotiation = update_variable_get('language_negotiation_' . $type, NULL); if (!empty($negotiation)) { - foreach ($negotiation as $method_id => &$method) { - if (isset($method['callbacks']['language'])) { - $method['callbacks']['negotiation'] = $method['callbacks']['language']; - unset($method['callbacks']['language']); - } - if (isset($method['callbacks']['switcher'])) { - $method['callbacks']['language_switch'] = $method['callbacks']['switcher']; - unset($method['callbacks']['switcher']); - } - } - update_variable_set('language_negotiation_' . $type, $negotiation); + update_variable_set('language_negotiation_' . $type, array_keys($negotiation)); } // Rename the language negotiation methods weight variable. @@ -647,16 +637,11 @@ function locale_update_8005() { } /** - * Convert language_negotiation_* variables to use the new callbacks. + * Convert language_negotiation_* variables. * * @ingroup config_upgrade */ function locale_update_8007() { - $variable_names = array( - 'language_negotiation_language_interface', - 'language_negotiation_language_content', - 'language_negotiation_language_url', - ); // Add all language type weight variables. As the function language_types() // is not available its functionality is rebuild. $language_types = update_variable_get('language_types', array( @@ -665,20 +650,9 @@ function locale_update_8007() { Language::TYPE_URL => FALSE, )); foreach ($language_types as $language_type => $configurable) { - $variable_names[] = 'language_negotiation_methods_weight_' . $language_type; + $variable_names['language_negotiation_' . $language_type] = 'language_negotiation_methods_weight_' . $language_type; } - $callback_map = array( - 'locale_language_from_url' => 'language_from_url', - 'locale_language_switcher_url' => 'language_switcher_url', - 'locale_language_url_rewrite_url' => 'language_url_rewrite_url', - 'locale_language_from_session' => 'language_from_session', - 'locale_language_switcher_session' => 'language_switcher_session', - 'locale_language_url_rewrite_session' => 'language_url_rewrite_session', - 'locale_language_from_user' => 'language_from_user', - 'locale_language_from_browser' => 'language_from_browser', - 'locale_language_url_fallback' => 'language_url_fallback', - 'locale_language_from_interface' => 'language_from_interface', - ); + $type_map = array( 'locale-interface' => 'language-interface', 'locale-url' => 'language-url', @@ -687,37 +661,24 @@ function locale_update_8007() { 'locale-user' => 'language-user', 'locale-session' => 'language-session', ); - foreach ($variable_names as $variable_name) { - $value = update_variable_get($variable_name); + foreach ($variable_names as $variable_name => $weight_variable_name) { + $value = update_variable_get($weight_variable_name); // Skip processing if the variable is not stored in the db. if ($value === NULL) { + update_variable_del($variable_name); continue; } - $new_value = $value; - foreach ($value as $type => $type_settings) { - // Convert the file. - if (isset($type_settings['file']) && (strpos($type_settings['file'], 'core/includes/locale.inc') !== FALSE)) { - $new_value[$type]['file'] = 'core/modules/language/language.negotiation.inc'; - } - // Convert the callbacks. - if (is_array($type_settings) && isset($type_settings['callbacks'])) { - foreach ($type_settings['callbacks'] as $key => $callback) { - if (isset($callback_map[$callback])) { - $new_value[$type]['callbacks'][$key] = $callback_map[$callback]; - } - } - } + $new_value = array(); + foreach ($value as $type => $weight) { // Convert the type. if (isset($type_map[$type])) { - $new_value[$type_map[$type]] = $new_value[$type]; - unset($new_value[$type]); + $type = $type_map[$type]; } + $new_value[$type] = $weight; } - // If necessary maintain the order of the values / keys of the variable. - if (stristr($variable_name, 'language_negotiation_methods_weight_') !== FALSE) { - asort($new_value); - } + asort($new_value); update_variable_set($variable_name, $new_value); + update_variable_del($weight_variable_name); } } @@ -846,21 +807,11 @@ function locale_update_8011() { * Renames language_default language negotiation method to language_selected. */ function locale_update_8013() { - $weight = update_variable_get('language_negotiation_methods_weight_language_interface', NULL); + $weight = update_variable_get('language_negotiation_language_interface', NULL); if ($weight !== NULL) { $weight[LanguageNegotiationSelected::METHOD_ID] = $weight['language-default']; unset($weight['language-default']); - update_variable_set('language_negotiation_methods_weight_language_interface', $weight); - } - - $negotiation_interface = update_variable_get('language_negotiation_language_interface', NULL); - if ($negotiation_interface !== NULL) { - if (isset($negotiation_interface['language-default'])) { - $negotiation_interface[LanguageNegotiationSelected::METHOD_ID] = $negotiation_interface['language-default']; - $negotiation_interface[LanguageNegotiationSelected::METHOD_ID]['callbacks']['negotiation'] = 'language_from_selected'; - unset($negotiation_interface['language-default']); - update_variable_set('language_negotiation_language_interface', $negotiation_interface); - } + update_variable_set('language_negotiation_language_interface', $weight); } } diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php b/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php index e5216b3..955ba12 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php @@ -7,6 +7,7 @@ namespace Drupal\node\Tests; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; use Drupal\simpletest\WebTestBase; use Drupal\Core\Language\Language; diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/LanguageUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/LanguageUpgradePathTest.php index 358a2f9..d99beac 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/LanguageUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/LanguageUpgradePathTest.php @@ -122,15 +122,11 @@ public function testLanguageUpgrade() { ); // Check that locale_language_providers_weight_language is correctly // renamed. - $current_weights = update_variable_get('language_negotiation_methods_weight_language_interface', array()); + $current_weights = update_variable_get('language_negotiation_language_interface', array()); $this->assertTrue(serialize($expected_weights) == serialize($current_weights), 'Language negotiation method weights upgraded.'); $this->assertTrue(isset($current_weights['language-selected']), 'Language-selected is present.'); $this->assertFalse(isset($current_weights['language-default']), 'Language-default is not present.'); - // Check that negotiation callback was added to language_negotiation_language_interface. - $language_negotiation_language_interface = update_variable_get('language_negotiation_language_interface', NULL); - $this->assertTrue(isset($language_negotiation_language_interface[LanguageNegotiationSelected::METHOD_ID]['callbacks']['negotiation']), 'Negotiation callback was added to language_negotiation_language_interface.'); - // Look up migrated plural string. $source_string = db_query('SELECT * FROM {locales_source} WHERE lid = 22')->fetchObject(); $this->assertEqual($source_string->source, implode(LOCALE_PLURAL_DELIMITER, array('1 byte', '@count bytes')));