From b979e8aa936036ec876545fe77e94fb1c5aa7b9f Mon Sep 17 00:00:00 2001 From: Katherine Bailey <katherine@katbailey.net> Date: Mon, 6 Aug 2012 11:59:53 -0700 Subject: [PATCH] Passing the request object explicitly to the language negotiation methods instead of a vague args parameter --- core/includes/language.inc | 17 ++++++++++------- .../Drupal/Core/Language/LanguageManager.php | 2 +- core/modules/language/language.negotiation.inc | 12 +++++++----- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/core/includes/language.inc b/core/includes/language.inc index 48ba3d6941e3..a243d2eff976 100644 --- a/core/includes/language.inc +++ b/core/includes/language.inc @@ -107,10 +107,13 @@ * @param $type * The language type key to find the language for. * + * @param $request + * The HttpReqeust object representing the current request. + * * @return * The negotiated language object. */ -function language_types_initialize($type, $args = array()) { +function language_types_initialize($type, $request = NULL) { // Execute the language negotiation methods in the order they were set up and // return the first valid language found. $negotiation = variable_get("language_negotiation_$type", array()); @@ -120,8 +123,7 @@ function language_types_initialize($type, $args = array()) { if (isset($method['types']) && !in_array($type, $method['types'])) { continue; } - $method['args'] = $args; - $language = language_negotiation_method_invoke($method_id, $method); + $language = language_negotiation_method_invoke($method_id, $method, $request); if ($language) { // Remember the method ID used to detect the language. $language->method_id = $method_id; @@ -429,10 +431,13 @@ function language_negotiation_info() { * invoked (see hook_language_negotiation_info() for details). If not passed * in, it will be loaded through language_negotiation_info(). * + * @param $request + * (optional) The HttpRequest object representing the current request. + * * @return * A language object representing the language chosen by the method. */ -function language_negotiation_method_invoke($method_id, $method = NULL) { +function language_negotiation_method_invoke($method_id, $method = NULL, $request = NULL) { $results = &drupal_static(__FUNCTION__); if (!isset($results[$method_id])) { @@ -448,13 +453,11 @@ function language_negotiation_method_invoke($method_id, $method = NULL) { if (isset($method['file'])) { require_once DRUPAL_ROOT . '/' . $method['file']; } - // If the language negotiation method has no cache preference or this is // satisfied we can execute the callback. $cache = !isset($method['cache']) || $user->uid || $method['cache'] == variable_get('cache', 0); $callback = isset($method['callbacks']['negotiation']) ? $method['callbacks']['negotiation'] : FALSE; - $args = isset($method['args']) ? $method['args'] : array(); - $langcode = $cache && function_exists($callback) ? $callback($languages, $args) : FALSE; + $langcode = $cache && function_exists($callback) ? $callback($languages, $request) : FALSE; $results[$method_id] = isset($languages[$langcode]) ? $languages[$langcode] : FALSE; } diff --git a/core/lib/Drupal/Core/Language/LanguageManager.php b/core/lib/Drupal/Core/Language/LanguageManager.php index 7bc7606f1a70..7514b7435511 100644 --- a/core/lib/Drupal/Core/Language/LanguageManager.php +++ b/core/lib/Drupal/Core/Language/LanguageManager.php @@ -33,7 +33,7 @@ public function getLanguage($type) { // @todo Objectify the language system so that we don't have to do this. include_once DRUPAL_ROOT . '/core/includes/language.inc'; - $this->languages[$type] = language_types_initialize($type, array('request' => $this->request)); + $this->languages[$type] = language_types_initialize($type, $this->request); return $this->languages[$type]; } diff --git a/core/modules/language/language.negotiation.inc b/core/modules/language/language.negotiation.inc index 78f958b2ff4b..73d7e6ad27cf 100644 --- a/core/modules/language/language.negotiation.inc +++ b/core/modules/language/language.negotiation.inc @@ -200,7 +200,7 @@ function language_from_session($languages) { * @return * A valid language code on success, FALSE otherwise. */ -function language_from_url($languages, $args = array()) { +function language_from_url($languages, $request = NULL) { $language_url = FALSE; if (!language_negotiation_method_enabled(LANGUAGE_NEGOTIATION_URL)) { @@ -212,9 +212,7 @@ function language_from_url($languages, $args = array()) { // Language negotiation happens before the public function current_path() // is available. // @todo Refactor with Symfony's Request object. - if (isset($args['request'])) { - $request = $args['request']; - + if ($request) { $current_path = $request->attributes->get('system_path'); if (!isset($current_path)) { $current_path = trim($request->getPathInfo(), '/'); @@ -285,6 +283,10 @@ function language_from_url($languages, $args = array()) { * (optional) An array of valid language objects. This is passed by * language_negotiation_method_invoke() to every language method callback, * but it is not actually needed here. Defaults to NULL. + * + * @param $request + * (optional) The HttpRequest object representing the current request. + * * @param $language_type * (optional) The language type to fall back to. Defaults to the interface * language. @@ -292,7 +294,7 @@ function language_from_url($languages, $args = array()) { * @return * A valid language code. */ -function language_url_fallback($language = NULL, $args = array(), $language_type = LANGUAGE_TYPE_INTERFACE) { +function language_url_fallback($language = NULL, $request = NULL, $language_type = LANGUAGE_TYPE_INTERFACE) { $default = language_default(); $prefix = (variable_get('language_negotiation_url_part', LANGUAGE_NEGOTIATION_URL_PREFIX) == LANGUAGE_NEGOTIATION_URL_PREFIX); -- GitLab