diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index a477907..a042fd6 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -2373,6 +2373,9 @@ function language($type) { * * @return bool * TRUE if more than one language is enabled. + * + * @deprecated as of Drupal 8.0, use + * \Drupal::languageManager()->isMultilingual() instead. */ function language_multilingual() { return \Drupal::languageManager()->isMultilingual(); diff --git a/core/includes/language.inc b/core/includes/language.inc index 55c31be..6b7329e 100644 --- a/core/includes/language.inc +++ b/core/includes/language.inc @@ -8,16 +8,8 @@ */ use Drupal\Core\Language\Language; - -/** - * No language negotiation. The default language is used. - */ -const LANGUAGE_NEGOTIATION_SELECTED = 'language-selected'; - -/** - * The language is determined using the current interface language. - */ -const LANGUAGE_NEGOTIATION_INTERFACE = 'language-interface'; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationSelected; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUI; /** * @defgroup language_negotiation Language Negotiation API functionality @@ -89,8 +81,8 @@ * function mymodule_language_negotiation_info_alter(&$negotiation_info) { * // Replace the core function with our own function. * module_load_include('language', 'inc', 'language.negotiation'); - * $negotiation_info[LANGUAGE_NEGOTIATION_URL]['callbacks']['negotiation'] = 'mymodule_from_url'; - * $negotiation_info[LANGUAGE_NEGOTIATION_URL]['file'] = drupal_get_path('module', 'mymodule') . '/mymodule.module'; + * $negotiation_info[\Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::METHOD_ID]['callbacks']['negotiation'] = 'mymodule_from_url'; + * $negotiation_info[\Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::METHOD_ID]['file'] = drupal_get_path('module', 'mymodule') . '/mymodule.module'; * } * * function mymodule_from_url($languages) { @@ -189,7 +181,7 @@ function language_types_set(array $configurable_language_types) { // language negotiation settings, we use the values negotiated for the // interface language which should always be available. if (!$configurable && !empty($info['fixed'])) { - $method_weights = array(LANGUAGE_NEGOTIATION_INTERFACE); + $method_weights = array(LanguageNegotiationUI::METHOD_ID); $method_weights = array_flip($method_weights); language_negotiation_set($type, $method_weights); } @@ -234,7 +226,7 @@ function language_types_set(array $configurable_language_types) { */ function language_negotiation_method_get_first($type) { $negotiation = variable_get("language_negotiation_$type", array()); - return empty($negotiation) ? LANGUAGE_NEGOTIATION_SELECTED : key($negotiation); + return empty($negotiation) ? LanguageNegotiationSelected::METHOD_ID : key($negotiation); } /** @@ -273,6 +265,9 @@ function language_negotiation_method_enabled($method_id, $type = NULL) { * * @return * A keyed array of links ready to be themed. + * + * @deprecated as of 8.0, use + * \Drupal::languageManager()->getLanguageNegotiationSwitchLinks() instead. */ function language_negotiation_get_switch_links($type, $path) { return Drupal::languageManager()->getLanguageNegotiationSwitchLinks($type, $path); @@ -293,7 +288,7 @@ function language_negotiation_purge() { foreach (language_types_info() as $type => $type_info) { $weight = 0; $method_weights = array(); - foreach (variable_get("language_negotiation_$type", array()) as $method_id => $method) { + foreach (variable_get("language_negotiation_$type", array()) as $method_id) { if (isset($negotiation_info[$method_id])) { $method_weights[$method_id] = $weight++; } @@ -311,10 +306,7 @@ function language_negotiation_purge() { * An array of language negotiation method weights keyed by method ID. */ function language_negotiation_set($type, $method_weights) { - // Save only the necessary fields. - $method_fields = array('callbacks', 'file', 'cache'); - - $negotiation = array(); + $methods = array(); $negotiation_info = language_negotiation_info(); $default_types = language_types_get_configurable(); @@ -329,18 +321,12 @@ function language_negotiation_set($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])) { - $method_data = array(); - foreach ($method_fields as $field) { - if (isset($method[$field])) { - $method_data[$field] = $method[$field]; - } - } - $negotiation[$method_id] = $method_data; + $methods[$method_id] = $method_id; } } } - variable_set("language_negotiation_$type", $negotiation); + variable_set("language_negotiation_$type", $methods); } /** @@ -348,6 +334,10 @@ function language_negotiation_set($type, $method_weights) { * * @return * An array of language negotiation methods. + * + * @deprecated as of 8.0, use + * Drupal::service('plugin.manager.language_negotiation_method')->getDefinitions() + * instead. */ function language_negotiation_info() { return Drupal::service('plugin.manager.language_negotiation_method')->getDefinitions(); diff --git a/core/lib/Drupal/Core/Language/LanguageManager.php b/core/lib/Drupal/Core/Language/LanguageManager.php index 5e79465..371454b 100644 --- a/core/lib/Drupal/Core/Language/LanguageManager.php +++ b/core/lib/Drupal/Core/Language/LanguageManager.php @@ -196,7 +196,7 @@ public function initializeType($type) { * Returns the negotiation settings for the specified language type. * * @param string $type - * The type of the language to retireve the negotiation settings for. + * The type of the language to retrieve the negotiation settings for. * * @returns array * An array of language negotiation method identifiers ordered by method @@ -204,7 +204,7 @@ public function initializeType($type) { */ protected function getNegotiationForType($type) { // @todo convert to CMI https://drupal.org/node/1827038 - return array_keys(variable_get("language_negotiation_$type", array())); + return variable_get("language_negotiation_$type", array()); } /** @@ -318,9 +318,9 @@ public function getTypeDefaults() { */ function getLanguageNegotiationSwitchLinks($type, $path) { $links = FALSE; - $negotiation = variable_get("language_negotiation_$type", array()); + $negotiation = $this->getNegotiationForType($type); - foreach ($negotiation as $method_id => $method) { + foreach ($negotiation as $method_id) { $definition = $this->negotiatorManager->getDefinition($method_id); if (method_exists($definition['class'], 'languageSwitchLinks')) { $instance = $this->negotiatorManager->createInstance($method_id, $this->config); diff --git a/core/modules/content_translation/content_translation.install b/core/modules/content_translation/content_translation.install index bd817bc..a1ac73a 100644 --- a/core/modules/content_translation/content_translation.install +++ b/core/modules/content_translation/content_translation.install @@ -6,6 +6,7 @@ */ use Drupal\Core\Language\Language; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; /** * Implements hook_schema(). @@ -85,8 +86,6 @@ 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(LANGUAGE_NEGOTIATION_URL => 0)); } /** diff --git a/core/modules/language/language.admin.inc b/core/modules/language/language.admin.inc index e3c5450..d6dd22c 100644 --- a/core/modules/language/language.admin.inc +++ b/core/modules/language/language.admin.inc @@ -6,6 +6,7 @@ */ use Drupal\Core\Language\LanguageManager; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationSelected; /** * Prepare a language code list for unused predefined languages. @@ -138,7 +139,7 @@ function language_negotiation_configure_form_table(&$form, $type) { '#title_display' => 'invisible', '#default_value' => $enabled, ); - if ($method_id === LANGUAGE_NEGOTIATION_SELECTED) { + if ($method_id === LanguageNegotiationSelected::METHOD_ID) { $table_form['enabled'][$method_id]['#default_value'] = TRUE; $table_form['enabled'][$method_id]['#attributes'] = array('disabled' => 'disabled'); } @@ -246,7 +247,7 @@ function language_negotiation_configure_form_submit($form, &$form_state) { $customized[$type] = in_array($type, $stored_values); $method_weights = array(); $enabled_methods = $form_state['values'][$type]['enabled']; - $enabled_methods[LANGUAGE_NEGOTIATION_SELECTED] = TRUE; + $enabled_methods[LanguageNegotiationSelected::METHOD_ID] = TRUE; $method_weights_input = $form_state['values'][$type]['weight']; if (isset($form_state['values'][$type]['configurable'])) { $customized[$type] = !empty($form_state['values'][$type]['configurable']); diff --git a/core/modules/language/language.install b/core/modules/language/language.install index 057f795..847dc40 100644 --- a/core/modules/language/language.install +++ b/core/modules/language/language.install @@ -6,6 +6,7 @@ */ use Drupal\Core\Language\Language; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; /** * Implements hook_install(). @@ -18,7 +19,7 @@ function language_install() { require_once DRUPAL_ROOT . '/core/includes/language.inc'; foreach (language_types_get_configurable() as $type) { module_load_include('inc', 'language', 'language.negotiation'); - language_negotiation_set($type, array(LANGUAGE_NEGOTIATION_URL => 0)); + language_negotiation_set($type, array(LanguageNegotiationUrl::METHOD_ID => 0)); } // Update the language count. @@ -34,8 +35,6 @@ function language_uninstall() { \Drupal::state()->delete('language_count'); // Clear variables. - variable_del('language_types'); - foreach (Drupal::languageManager()->getLanguageTypes() as $type) { variable_del("language_negotiation_$type"); variable_del("language_negotiation_methods_weight_$type"); diff --git a/core/modules/language/language.module b/core/modules/language/language.module index 500e2ff..05f5b56 100644 --- a/core/modules/language/language.module +++ b/core/modules/language/language.module @@ -5,6 +5,9 @@ * Add language handling functionality to Drupal. */ +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUI; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrlFallback; use Drupal\node\NodeTypeInterface; use Drupal\Core\Language\Language; @@ -625,11 +628,11 @@ function language_language_types_info() { Language::TYPE_CONTENT => array( 'name' => t('Content'), 'description' => t('Order of language detection methods for content. If a version of content is available in the detected language, it will be displayed.'), - 'fixed' => array(LANGUAGE_NEGOTIATION_INTERFACE), + 'fixed' => array(LanguageNegotiationUI::METHOD_ID), 'locked' => TRUE, ), Language::TYPE_URL => array( - 'fixed' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_URL_FALLBACK), + 'fixed' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationUrlFallback::METHOD_ID), 'locked' => TRUE, ), ); diff --git a/core/modules/language/language.negotiation.inc b/core/modules/language/language.negotiation.inc index d130bdb..4e1ff69 100644 --- a/core/modules/language/language.negotiation.inc +++ b/core/modules/language/language.negotiation.inc @@ -1,54 +1,12 @@ get('session.parameter')); $query_value = isset($_GET[$query_param]) ? check_plain($_GET[$query_param]) : NULL; - $query_rewrite = isset($languages[$query_value]) && language_negotiation_method_enabled(LANGUAGE_NEGOTIATION_SESSION); + $query_rewrite = isset($languages[$query_value]) && language_negotiation_method_enabled(LanguageNegotiationSession::METHOD_ID); } else { $query_rewrite = FALSE; diff --git a/core/modules/language/lib/Drupal/language/Form/NegotiationBrowserForm.php b/core/modules/language/lib/Drupal/language/Form/NegotiationBrowserForm.php index b801b2b..01ae858 100644 --- a/core/modules/language/lib/Drupal/language/Form/NegotiationBrowserForm.php +++ b/core/modules/language/lib/Drupal/language/Form/NegotiationBrowserForm.php @@ -177,6 +177,7 @@ public function submitForm(array &$form, array &$form_state) { $config = $this->configFactory->get('language.mappings'); $config->setData($mappings); $config->save(); + drupal_rebuild_language_negotiation_settings(); } $form_state['redirect'] = 'admin/config/regional/language/detection'; diff --git a/core/modules/language/lib/Drupal/language/Form/NegotiationSessionForm.php b/core/modules/language/lib/Drupal/language/Form/NegotiationSessionForm.php index 1c73492..b937086c 100644 --- a/core/modules/language/lib/Drupal/language/Form/NegotiationSessionForm.php +++ b/core/modules/language/lib/Drupal/language/Form/NegotiationSessionForm.php @@ -46,6 +46,8 @@ public function submitForm(array &$form, array &$form_state) { ->set('session.parameter', $form_state['values']['language_negotiation_session_param']) ->save(); + drupal_rebuild_language_negotiation_settings(); + parent::submitForm($form, $form_state); } diff --git a/core/modules/language/lib/Drupal/language/Form/NegotiationUrlForm.php b/core/modules/language/lib/Drupal/language/Form/NegotiationUrlForm.php index 6855a24..5c8fb7d 100644 --- a/core/modules/language/lib/Drupal/language/Form/NegotiationUrlForm.php +++ b/core/modules/language/lib/Drupal/language/Form/NegotiationUrlForm.php @@ -8,6 +8,7 @@ namespace Drupal\language\Form; use Drupal\Core\Form\ConfigFormBase; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; /** * Configure the URL language negotiation method for this site. @@ -33,8 +34,8 @@ public function buildForm(array $form, array &$form_state) { '#title' => t('Part of the URL that determines language'), '#type' => 'radios', '#options' => array( - LANGUAGE_NEGOTIATION_URL_PREFIX => t('Path prefix'), - LANGUAGE_NEGOTIATION_URL_DOMAIN => t('Domain'), + LanguageNegotiationUrl::CONFIG_PATH_PREFIX => t('Path prefix'), + LanguageNegotiationUrl::CONFIG_DOMAIN => t('Domain'), ), '#default_value' => $config->get('url.source'), ); @@ -47,7 +48,7 @@ public function buildForm(array $form, array &$form_state) { '#states' => array( 'visible' => array( ':input[name="language_negotiation_url_part"]' => array( - 'value' => (string) LANGUAGE_NEGOTIATION_URL_PREFIX, + 'value' => (string) LanguageNegotiationUrl::CONFIG_PATH_PREFIX, ), ), ), @@ -60,7 +61,7 @@ public function buildForm(array $form, array &$form_state) { '#states' => array( 'visible' => array( ':input[name="language_negotiation_url_part"]' => array( - 'value' => (string) LANGUAGE_NEGOTIATION_URL_DOMAIN, + 'value' => (string) LanguageNegotiationUrl::CONFIG_DOMAIN, ), ), ), @@ -103,7 +104,7 @@ public function validateForm(array &$form, array &$form_state) { $value = $form_state['values']['prefix'][$langcode]; if ($value === '') { - if (!$language->default && $form_state['values']['language_negotiation_url_part'] == LANGUAGE_NEGOTIATION_URL_PREFIX) { + if (!$language->default && $form_state['values']['language_negotiation_url_part'] == LanguageNegotiationUrl::CONFIG_PATH_PREFIX) { // Throw a form error if the prefix is blank for a non-default language, // although it is required for selected negotiation type. form_error($form['prefix'][$langcode], t('The prefix may only be left blank for the default language.')); @@ -127,7 +128,7 @@ public function validateForm(array &$form, array &$form_state) { $value = $form_state['values']['domain'][$langcode]; if ($value === '') { - if (!$language->default && $form_state['values']['language_negotiation_url_part'] == LANGUAGE_NEGOTIATION_URL_DOMAIN) { + if (!$language->default && $form_state['values']['language_negotiation_url_part'] == LanguageNegotiationUrl::CONFIG_DOMAIN) { // Throw a form error if the domain is blank for a non-default language, // although it is required for selected negotiation type. form_error($form['domain'][$langcode], t('The domain may only be left blank for the default language.')); diff --git a/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php b/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php index 5279dd6..3a5bbed 100644 --- a/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php +++ b/core/modules/language/lib/Drupal/language/Plugin/Block/LanguageBlock.php @@ -10,6 +10,9 @@ use Drupal\block\BlockBase; use Drupal\block\Annotation\Block; use Drupal\Core\Annotation\Translation; +use Drupal\Core\Language\LanguageManager; +use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a 'Language switcher' block. @@ -20,13 +23,51 @@ * derivative = "Drupal\language\Plugin\Derivative\LanguageBlock" * ) */ -class LanguageBlock extends BlockBase { +class LanguageBlock extends BlockBase implements ContainerFactoryPluginInterface { + + /** + * The language manager. + * + * @var \Drupal\Core\Language\LanguageManager + */ + protected $languageManager; + + /** + * Constructs an LanguageBlock object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param array $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\Language\LanguageManager $language_manager + * The language manager. + */ + public function __construct(array $configuration, $plugin_id, array $plugin_definition, LanguageManager $language_manager) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->languageManager = $language_manager; + } + + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('language_manager') + ); + } + /** * Overrides \Drupal\block\BlockBase::access(). */ function access() { - return language_multilingual(); + return $this->languageManager->isMultilingual(); } /** @@ -36,7 +77,7 @@ public function build() { $build = array(); $path = drupal_is_front_page() ? '' : current_path(); list(, $type) = explode(':', $this->getPluginId()); - $links = language_negotiation_get_switch_links($type, $path); + $links = $this->languageManager->getLanguageNegotiationSwitchLinks($type, $path); if (isset($links->links)) { $build = array( diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php index 4698543..bf25a46 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageNegotiationInfoTest.php @@ -8,6 +8,7 @@ namespace Drupal\language\Tests; use Drupal\Core\Language\Language; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUI; use Drupal\simpletest\WebTestBase; /** @@ -62,7 +63,7 @@ function testInfoAlterations() { // Enable some core and custom language negotiation methods. The test // language type is supposed to be configurable. $test_type = 'test_language_type'; - $interface_method_id = LANGUAGE_NEGOTIATION_INTERFACE; + $interface_method_id = LanguageNegotiationUI::METHOD_ID; $test_method_id = 'test_language_negotiation_method'; $form_field = $type . '[enabled]['. $interface_method_id .']'; $edit = array( diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php index c418276..0bcebb0 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageUILanguageNegotiationTest.php @@ -7,6 +7,11 @@ namespace Drupal\language\Tests; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationBrowser; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationSelected; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUser; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin; use Drupal\simpletest\WebTestBase; use Drupal\Core\Language\Language; use Symfony\Component\HttpFoundation\Request; @@ -146,93 +151,95 @@ function testUILanguageNegotiation() { $edit = array('selected_langcode' => $langcode); $this->drupalPostForm('admin/config/regional/language/detection/selected', $edit, t('Save configuration')); $test = array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $language_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED, + 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, 'http_header' => $http_header_browser_fallback, 'message' => 'SELECTED: UI language is switched based on selected language.', ); - $this->runTest($test); + $this->assertNegotation($test); // An invalid language is selected. \Drupal::config('language.negotiation')->set('selected_langcode', NULL)->save(); + drupal_rebuild_language_negotiation_settings(); $test = array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED, + 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, 'http_header' => $http_header_browser_fallback, 'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.', ); - $this->runTest($test); + $this->assertNegotation($test); // No selected language is available. \Drupal::config('language.negotiation')->set('selected_langcode', $langcode_unknown)->save(); + drupal_rebuild_language_negotiation_settings(); $test = array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED, + 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, 'http_header' => $http_header_browser_fallback, 'message' => 'SELECTED > DEFAULT: UI language is switched based on selected language.', ); - $this->runTest($test); + $this->assertNegotation($test); $tests = array( // Default, browser preference should have no influence. array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED, + 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, 'http_header' => $http_header_browser_fallback, 'message' => 'URL (PATH) > DEFAULT: no language prefix, UI language is default and the browser language preference setting is not used.', ), // Language prefix. array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => "$langcode/admin/config", 'expect' => $language_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_URL, + 'expected_method_id' => LanguageNegotiationUrl::METHOD_ID, 'http_header' => $http_header_browser_fallback, 'message' => 'URL (PATH) > DEFAULT: with language prefix, UI language is switched based on path prefix', ), // Default, go by browser preference. array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER), + 'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID), 'path' => 'admin/config', 'expect' => $language_browser_fallback_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_BROWSER, + 'expected_method_id' => LanguageNegotiationBrowser::METHOD_ID, 'http_header' => $http_header_browser_fallback, 'message' => 'URL (PATH) > BROWSER: no language prefix, UI language is determined by browser language preference', ), // Prefix, switch to the language. array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER), + 'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID), 'path' => "$langcode/admin/config", 'expect' => $language_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_URL, + 'expected_method_id' => LanguageNegotiationUrl::METHOD_ID, 'http_header' => $http_header_browser_fallback, 'message' => 'URL (PATH) > BROWSER: with language prefix, UI language is based on path prefix', ), // Default, browser language preference is not one of site's lang. array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_BROWSER, LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationUrl::METHOD_ID, LanguageNegotiationBrowser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED, + 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, 'http_header' => $http_header_blah, 'message' => 'URL (PATH) > BROWSER > DEFAULT: no language prefix and browser language preference set to unknown language should use default language', ), ); foreach ($tests as $test) { - //$this->runTest($test); + $this->assertNegotation($test); } // Unknown language prefix should return 404. - variable_set('language_negotiation_' . Language::TYPE_INTERFACE, language_negotiation_info()); + variable_set('language_negotiation_' . Language::TYPE_INTERFACE, array_keys(language_negotiation_info())); $this->drupalGet("$langcode_unknown/admin/config", array(), $http_header_browser_fallback); $this->assertResponse(404, "Unknown language path prefix should return 404"); @@ -242,14 +249,14 @@ function testUILanguageNegotiation() { $account->save(); $test = array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER, LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED, + 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, 'http_header' => array(), 'message' => 'USER > DEFAULT: no preferred user language setting, the UI language is default', ); - $this->runTest($test); + $this->assertNegotation($test); // Set preferred langcode for user to unknown language. $account = $this->loggedInUser; @@ -257,74 +264,74 @@ function testUILanguageNegotiation() { $account->save(); $test = array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER, LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED, + 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, 'http_header' => array(), 'message' => 'USER > DEFAULT: invalid preferred user language setting, the UI language is default', ); - $this->runTest($test); + $this->assertNegotation($test); // Set preferred langcode for user to non default. $account->preferred_langcode = $langcode; $account->save(); $test = array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER, LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationUser::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $language_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_USER, + 'expected_method_id' => LanguageNegotiationUser::METHOD_ID, 'http_header' => array(), 'message' => 'USER > DEFAULT: defined prefereed user language setting, the UI language is based on user setting', ); - $this->runTest($test); + $this->assertNegotation($test); // Set preferred admin langcode for user to NULL. $account->preferred_admin_langcode = NULL; $account->save(); $test = array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER_ADMIN, LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED, + 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, 'http_header' => array(), 'message' => 'USER ADMIN > DEFAULT: no preferred user admin language setting, the UI language is default', ); - $this->runTest($test); + $this->assertNegotation($test); // Set preferred admin langcode for user to unknown language. $account->preferred_admin_langcode = $langcode_unknown; $account->save(); $test = array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER_ADMIN, LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $default_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_SELECTED, + 'expected_method_id' => LanguageNegotiationSelected::METHOD_ID, 'http_header' => array(), 'message' => 'USER ADMIN > DEFAULT: invalid preferred user admin language setting, the UI language is default', ); - $this->runTest($test); + $this->assertNegotation($test); // Set preferred admin langcode for user to non default. $account->preferred_admin_langcode = $langcode; $account->save(); $test = array( - 'language_negotiation' => array(LANGUAGE_NEGOTIATION_USER_ADMIN, LANGUAGE_NEGOTIATION_SELECTED), + 'language_negotiation' => array(LanguageNegotiationUserAdmin::METHOD_ID, LanguageNegotiationSelected::METHOD_ID), 'path' => 'admin/config', 'expect' => $language_string, - 'expected_method_id' => LANGUAGE_NEGOTIATION_USER_ADMIN, + 'expected_method_id' => LanguageNegotiationUserAdmin::METHOD_ID, 'http_header' => array(), 'message' => 'USER ADMIN > DEFAULT: defined prefereed user admin language setting, the UI language is based on user setting', ); - $this->runTest($test); + $this->assertNegotation($test); } - protected function runTest($test) { + protected function assertNegotation($test) { if (!empty($test['language_negotiation'])) { $method_weights = array_flip($test['language_negotiation']); language_negotiation_set(Language::TYPE_INTERFACE, $method_weights); @@ -333,11 +340,11 @@ protected function runTest($test) { \Drupal::config('language.negotiation') ->set('url.source', $test['language_negotiation_url_part']) ->save(); + drupal_rebuild_language_negotiation_settings(); } if (!empty($test['language_test_domain'])) { \Drupal::state()->set('language_test.domain', $test['language_test_domain']); } - drupal_rebuild_language_negotiation_settings(); $this->container->get('language_manager')->reset(); $this->drupalGet($test['path'], array(), $test['http_header']); $this->assertText($test['expect'], $test['message']); @@ -412,7 +419,7 @@ function testLanguageDomain() { // Change the domain for the Italian language. $edit = array( - 'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN, + 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN, 'domain[it]' => 'it.example.com', ); $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); diff --git a/core/modules/language/lib/Drupal/language/Tests/LanguageUrlRewritingTest.php b/core/modules/language/lib/Drupal/language/Tests/LanguageUrlRewritingTest.php index c30f55b..5c9b91c 100644 --- a/core/modules/language/lib/Drupal/language/Tests/LanguageUrlRewritingTest.php +++ b/core/modules/language/lib/Drupal/language/Tests/LanguageUrlRewritingTest.php @@ -7,6 +7,7 @@ namespace Drupal\language\Tests; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; use Drupal\simpletest\WebTestBase; use Symfony\Component\HttpFoundation\Request; @@ -103,7 +104,7 @@ private function checkUrl($language, $message1, $message2) { function testDomainNameNegotiationPort() { $language_domain = 'example.fr'; $edit = array( - 'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN, + 'language_negotiation_url_part' => LanguageNegotiationUrl::CONFIG_DOMAIN, 'domain[fr]' => $language_domain ); $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); @@ -113,7 +114,7 @@ function testDomainNameNegotiationPort() { // Enable domain configuration. \Drupal::config('language.negotiation') - ->set('url.source', LANGUAGE_NEGOTIATION_URL_DOMAIN) + ->set('url.source', LanguageNegotiationUrl::CONFIG_DOMAIN) ->save(); // Reset static caching. diff --git a/core/modules/language/tests/language_test/language_test.module b/core/modules/language/tests/language_test/language_test.module index 3c0fa2e..3b6669c 100644 --- a/core/modules/language/tests/language_test/language_test.module +++ b/core/modules/language/tests/language_test/language_test.module @@ -5,10 +5,8 @@ * Mock module for language layer tests. */ -use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpKernel\HttpKernelInterface; - use Drupal\Core\Language\Language; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUI; /** * Implements hook_page_build(). @@ -60,7 +58,7 @@ function language_test_language_types_info_alter(array &$language_types) { */ function language_test_language_negotiation_info_alter(array &$negotiation_info) { if (\Drupal::state()->get('language_test.language_negotiation_info_alter')) { - unset($negotiation_info[LANGUAGE_NEGOTIATION_INTERFACE]); + unset($negotiation_info[LanguageNegotiationUI::METHOD_ID]); } } diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php index 89de8fc..98e8573 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php @@ -7,6 +7,8 @@ namespace Drupal\locale\Tests; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationSelected; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; use Drupal\simpletest\WebTestBase; use Drupal\Core\Language\Language; use Drupal\Component\Utility\String; @@ -97,7 +99,7 @@ function testUninstallProcess() { // Change language negotiation settings. \Drupal::config('language.negotiation') - ->set('url.source', LANGUAGE_NEGOTIATION_URL_PREFIX) + ->set('url.source', LanguageNegotiationUrl::CONFIG_PATH_PREFIX) ->set('session.parameter', TRUE) ->save(); @@ -120,11 +122,11 @@ function testUninstallProcess() { // Check language negotiation. require_once DRUPAL_ROOT . '/core/includes/language.inc'; $this->assertTrue(count($this->container->get('language_manager')->getLanguageTypes()) == count($this->container->get('language_manager')->getTypeDefaults()), 'Language types reset'); - $language_negotiation = language_negotiation_method_get_first(Language::TYPE_INTERFACE) == LANGUAGE_NEGOTIATION_SELECTED; + $language_negotiation = language_negotiation_method_get_first(Language::TYPE_INTERFACE) == LanguageNegotiationSelected::METHOD_ID; $this->assertTrue($language_negotiation, String::format('Interface language negotiation: %setting', array('%setting' => $language_negotiation ? 'none' : 'set'))); - $language_negotiation = language_negotiation_method_get_first(Language::TYPE_CONTENT) == LANGUAGE_NEGOTIATION_SELECTED; + $language_negotiation = language_negotiation_method_get_first(Language::TYPE_CONTENT) == LanguageNegotiationSelected::METHOD_ID; $this->assertTrue($language_negotiation, String::format('Content language negotiation: %setting', array('%setting' => $language_negotiation ? 'none' : 'set'))); - $language_negotiation = language_negotiation_method_get_first(Language::TYPE_URL) == LANGUAGE_NEGOTIATION_SELECTED; + $language_negotiation = language_negotiation_method_get_first(Language::TYPE_URL) == LanguageNegotiationSelected::METHOD_ID; $this->assertTrue($language_negotiation, String::format('URL language negotiation: %setting', array('%setting' => $language_negotiation ? 'none' : 'set'))); // Check language negotiation method settings. diff --git a/core/modules/locale/locale.install b/core/modules/locale/locale.install index 34c4f91..d1a77d7 100644 --- a/core/modules/locale/locale.install +++ b/core/modules/locale/locale.install @@ -6,6 +6,7 @@ */ use Drupal\Core\Language\Language; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationSelected; /** * Implements hook_install(). @@ -845,13 +846,9 @@ function locale_update_8011() { * Renames language_default language negotiation method to language_selected. */ function locale_update_8013() { - // @todo We only need language.inc here because LANGUAGE_NEGOTIATION_SELECTED - // is defined there. Remove this line once that has been converted to a class - // constant. - require_once DRUPAL_ROOT . '/core/includes/language.inc'; $weight = update_variable_get('language_negotiation_methods_weight_language_interface', NULL); if ($weight !== NULL) { - $weight[LANGUAGE_NEGOTIATION_SELECTED] = $weight['language-default']; + $weight[LanguageNegotiationSelected::METHOD_ID] = $weight['language-default']; unset($weight['language-default']); update_variable_set('language_negotiation_methods_weight_language_interface', $weight); } @@ -859,8 +856,8 @@ function locale_update_8013() { $negotiation_interface = update_variable_get('language_negotiation_language_interface', NULL); if ($negotiation_interface !== NULL) { if (isset($negotiation_interface['language-default'])) { - $negotiation_interface[LANGUAGE_NEGOTIATION_SELECTED] = $negotiation_interface['language-default']; - $negotiation_interface[LANGUAGE_NEGOTIATION_SELECTED]['callbacks']['negotiation'] = 'language_from_selected'; + $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); } diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php b/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php index e6626ca..e5216b3 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeFieldMultilingualTestCase.php @@ -99,7 +99,7 @@ function testMultilingualNodeForm() { $this->assertTrue($node->language()->id == $langcode && $node->body->value == $body_value, 'Field language correctly changed.'); // Enable content language URL detection. - language_negotiation_set(Language::TYPE_CONTENT, array(LANGUAGE_NEGOTIATION_URL => 0)); + language_negotiation_set(Language::TYPE_CONTENT, array(LanguageNegotiationUrl::METHOD_ID => 0)); // Test multilingual field language fallback logic. $this->drupalGet("it/node/{$node->id()}"); diff --git a/core/modules/overlay/overlay.module b/core/modules/overlay/overlay.module index 912e29d..2e67a93 100644 --- a/core/modules/overlay/overlay.module +++ b/core/modules/overlay/overlay.module @@ -5,6 +5,7 @@ * Displays the Drupal administration interface in an overlay. */ +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; use Drupal\Core\Template\RenderWrapper; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; @@ -503,7 +504,7 @@ function overlay_overlay_parent_initialize() { $path_prefixes = array(); if (module_exists('language')) { language_negotiation_include(); - if (\Drupal::config('language.negotiation')->get('url.source') == LANGUAGE_NEGOTIATION_URL_PREFIX) { + if (\Drupal::config('language.negotiation')->get('url.source') == LanguageNegotiationUrl::CONFIG_PATH_PREFIX) { // Skip the empty string indicating the default language. We always accept // paths without a prefix. $path_prefixes = language_negotiation_url_prefixes(); diff --git a/core/modules/system/language.api.php b/core/modules/system/language.api.php index a7e6d13..e7328d3 100644 --- a/core/modules/system/language.api.php +++ b/core/modules/system/language.api.php @@ -49,7 +49,7 @@ function hook_language_switch_links_alter(array &$links, $type, $path) { * - fixed: A fixed array of language negotiation method identifiers to use to * initialize this language. If locked is set to TRUE and fixed is set, it * will always use the specified methods in the given priority order. If not - * present and locked is TRUE then LANGUAGE_NEGOTIATION_INTERFACE will be + * present and locked is TRUE then language-interface will be * used. * * @todo Rename the 'fixed' key to something more meaningful, for instance 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 df27013..358a2f9 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/LanguageUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/LanguageUpgradePathTest.php @@ -10,6 +10,7 @@ use Drupal\Core\Database\DatabaseException; use Drupal\Core\Language\Language; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationSelected; /** * Tests upgrading a filled database with language data. @@ -126,14 +127,9 @@ public function testLanguageUpgrade() { $this->assertTrue(isset($current_weights['language-selected']), 'Language-selected is present.'); $this->assertFalse(isset($current_weights['language-default']), 'Language-default is not present.'); - // @todo We only need language.inc here because LANGUAGE_NEGOTIATION_SELECTED - // is defined there. Remove this line once that has been converted to a class - // constant. - require_once DRUPAL_ROOT . '/core/includes/language.inc'; - // 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[LANGUAGE_NEGOTIATION_SELECTED]['callbacks']['negotiation']), 'Negotiation callback was added to language_negotiation_language_interface.'); + $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(); diff --git a/core/modules/user/lib/Drupal/user/AccountFormController.php b/core/modules/user/lib/Drupal/user/AccountFormController.php index b9fe6e3..35c749e 100644 --- a/core/modules/user/lib/Drupal/user/AccountFormController.php +++ b/core/modules/user/lib/Drupal/user/AccountFormController.php @@ -9,6 +9,7 @@ use Drupal\Core\Entity\ContentEntityFormController; use Drupal\Core\Language\Language; +use Drupal\Core\Language\Plugin\LanguageNegotiation\LanguageNegotiationSelected; /** * Form controller for the user account forms. @@ -183,7 +184,7 @@ public function form(array $form, array &$form_state) { // Is default the interface language? include_once DRUPAL_ROOT . '/core/includes/language.inc'; - $interface_language_is_default = language_negotiation_method_get_first(Language::TYPE_INTERFACE) != LANGUAGE_NEGOTIATION_SELECTED; + $interface_language_is_default = language_negotiation_method_get_first(Language::TYPE_INTERFACE) != LanguageNegotiationSelected::METHOD_ID; $form['language'] = array( '#type' => language_multilingual() ? 'details' : 'container', '#title' => $this->t('Language settings'),