Commit 61f8fc24 authored by webchick's avatar webchick

Issue #1999388 by kim.pepper, wouter.adem, pwieck, penyaskito, chertzog,...

Issue #1999388 by kim.pepper, wouter.adem, pwieck, penyaskito, chertzog, herom: Use Symfony Request for language module.
parent 7a150d14
<?php
use Drupal\Component\Utility\String;
use \Symfony\Component\HttpFoundation\Request;
/**
......@@ -81,7 +82,8 @@ function language_from_interface() {
* A valid language code on success, FALSE otherwise.
*/
function language_from_browser($languages) {
if (empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$accept_language = \Drupal::request()->server->get('HTTP_ACCEPT_LANGUAGE');
if (empty($accept_language)) {
return FALSE;
}
......@@ -93,7 +95,7 @@ function language_from_browser($languages) {
// language-range = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
// Samples: "hu, en-us;q=0.66, en;q=0.33", "hu,en-us;q=0.5"
$browser_langcodes = array();
if (preg_match_all('@(?<=[, ]|^)([a-zA-Z-]+|\*)(?:;q=([0-9.]+))?(?:$|\s*,\s*)@', trim($_SERVER['HTTP_ACCEPT_LANGUAGE']), $matches, PREG_SET_ORDER)) {
if (preg_match_all('@(?<=[, ]|^)([a-zA-Z-]+|\*)(?:;q=([0-9.]+))?(?:$|\s*,\s*)@', trim($accept_language), $matches, PREG_SET_ORDER)) {
// Load custom mappings to support browsers that are sending non standard
// language codes.
$mappings = language_get_browser_drupal_langcode_mappings();
......@@ -250,10 +252,11 @@ function language_from_user_admin(array $languages, Request $request = NULL) {
*/
function language_from_session($languages) {
$param = \Drupal::config('language.negotiation')->get('session.parameter');
$query = \Drupal::request()->query;
// Request parameter: we need to update the session parameter only if we have
// an authenticated user.
if (isset($_GET[$param]) && isset($languages[$langcode = $_GET[$param]])) {
if ($query->has($param) && isset($languages[$langcode = $query->get($param)])) {
$user = \Drupal::currentUser();
if ($user->id()) {
$_SESSION[$param] = $langcode;
......@@ -302,7 +305,7 @@ function language_from_url($languages, Request $request = NULL) {
case LANGUAGE_NEGOTIATION_URL_DOMAIN:
// Get only the host, not the port.
$http_host= $_SERVER['HTTP_HOST'];
$http_host= \Drupal::request()->server->get('HTTP_HOST');
if (strpos($http_host, ':') !== FALSE) {
$http_host_tmp = explode(':', $http_host);
$http_host = current($http_host_tmp);
......@@ -412,7 +415,7 @@ function language_switcher_session($type, $path) {
$languages = language_list();
$links = array();
$query = $_GET;
$query = \Drupal::request()->query->all();
foreach ($languages as $language) {
$langcode = $language->id;
......@@ -495,8 +498,14 @@ function language_url_rewrite_session(&$path, &$options) {
$user = \Drupal::currentUser();
if (!$user->id()) {
$languages = language_list();
$query_param = check_plain(\Drupal::config('language.negotiation')->get('session.parameter'));
$query_value = isset($_GET[$query_param]) ? check_plain($_GET[$query_param]) : NULL;
$query_param = String::checkPlain(\Drupal::config('language.negotiation')->get('session.parameter'));
$query = \Drupal::request()->query;
if ($query->has($query_param)) {
$query_value = String::checkPlain(\Drupal::request()->query->get($query_param));
}
else {
return FALSE;
}
$query_rewrite = isset($languages[$query_value]) && language_negotiation_method_enabled(LANGUAGE_NEGOTIATION_SESSION);
}
else {
......
......@@ -154,7 +154,7 @@ function testLanguageFromBrowser() {
);
foreach ($test_cases as $accept_language => $expected_result) {
$_SERVER['HTTP_ACCEPT_LANGUAGE'] = $accept_language;
\Drupal::request()->server->set('HTTP_ACCEPT_LANGUAGE', $accept_language);
$result = language_from_browser($languages);
$this->assertIdentical($result, $expected_result, format_string("Language selection '@accept-language' selects '@result', result = '@actual'", array('@accept-language' => $accept_language, '@result' => $expected_result, '@actual' => isset($result) ? $result : 'none')));
}
......
......@@ -342,8 +342,9 @@ function testUILanguageNegotiation() {
'http_header' => $http_header_browser_fallback,
'message' => 'URL (DOMAIN) > DEFAULT: default domain should get default language',
),
// Language domain specific URL, we set the $_SERVER['HTTP_HOST'] in
// \Drupal\language_test\LanguageTestManager to simulate this.
// Language domain specific URL, we set the 'HTTP_HOST' property of
// \Drupal::request()->server in \Drupal\language_test\LanguageTestManager
// to simulate this.
array(
'language_negotiation' => array(LANGUAGE_NEGOTIATION_URL, LANGUAGE_NEGOTIATION_SELECTED),
'language_negotiation_url_part' => LANGUAGE_NEGOTIATION_URL_DOMAIN,
......
......@@ -20,7 +20,7 @@ class LanguageTestManager extends LanguageManager {
*/
public function init() {
if ($test_domain = \Drupal::state()->get('language_test.domain')) {
$_SERVER['HTTP_HOST'] = $test_domain;
\Drupal::request()->server->set('HTTP_HOST', $test_domain);
}
return parent::init();
}
......
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