Commit b979e8aa authored by katbailey's avatar katbailey
Browse files

Passing the request object explicitly to the language negotiation methods...

Passing the request object explicitly to the language negotiation methods instead of a vague args parameter
parent 93e3f5bf
...@@ -107,10 +107,13 @@ ...@@ -107,10 +107,13 @@
* @param $type * @param $type
* The language type key to find the language for. * The language type key to find the language for.
* *
* @param $request
* The HttpReqeust object representing the current request.
*
* @return * @return
* The negotiated language object. * 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 // Execute the language negotiation methods in the order they were set up and
// return the first valid language found. // return the first valid language found.
$negotiation = variable_get("language_negotiation_$type", array()); $negotiation = variable_get("language_negotiation_$type", array());
...@@ -120,8 +123,7 @@ function language_types_initialize($type, $args = array()) { ...@@ -120,8 +123,7 @@ function language_types_initialize($type, $args = array()) {
if (isset($method['types']) && !in_array($type, $method['types'])) { if (isset($method['types']) && !in_array($type, $method['types'])) {
continue; continue;
} }
$method['args'] = $args; $language = language_negotiation_method_invoke($method_id, $method, $request);
$language = language_negotiation_method_invoke($method_id, $method);
if ($language) { if ($language) {
// Remember the method ID used to detect the language. // Remember the method ID used to detect the language.
$language->method_id = $method_id; $language->method_id = $method_id;
...@@ -429,10 +431,13 @@ function language_negotiation_info() { ...@@ -429,10 +431,13 @@ function language_negotiation_info() {
* invoked (see hook_language_negotiation_info() for details). If not passed * invoked (see hook_language_negotiation_info() for details). If not passed
* in, it will be loaded through language_negotiation_info(). * in, it will be loaded through language_negotiation_info().
* *
* @param $request
* (optional) The HttpRequest object representing the current request.
*
* @return * @return
* A language object representing the language chosen by the method. * 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__); $results = &drupal_static(__FUNCTION__);
if (!isset($results[$method_id])) { if (!isset($results[$method_id])) {
...@@ -448,13 +453,11 @@ function language_negotiation_method_invoke($method_id, $method = NULL) { ...@@ -448,13 +453,11 @@ function language_negotiation_method_invoke($method_id, $method = NULL) {
if (isset($method['file'])) { if (isset($method['file'])) {
require_once DRUPAL_ROOT . '/' . $method['file']; require_once DRUPAL_ROOT . '/' . $method['file'];
} }
// If the language negotiation method has no cache preference or this is // If the language negotiation method has no cache preference or this is
// satisfied we can execute the callback. // satisfied we can execute the callback.
$cache = !isset($method['cache']) || $user->uid || $method['cache'] == variable_get('cache', 0); $cache = !isset($method['cache']) || $user->uid || $method['cache'] == variable_get('cache', 0);
$callback = isset($method['callbacks']['negotiation']) ? $method['callbacks']['negotiation'] : FALSE; $callback = isset($method['callbacks']['negotiation']) ? $method['callbacks']['negotiation'] : FALSE;
$args = isset($method['args']) ? $method['args'] : array(); $langcode = $cache && function_exists($callback) ? $callback($languages, $request) : FALSE;
$langcode = $cache && function_exists($callback) ? $callback($languages, $args) : FALSE;
$results[$method_id] = isset($languages[$langcode]) ? $languages[$langcode] : FALSE; $results[$method_id] = isset($languages[$langcode]) ? $languages[$langcode] : FALSE;
} }
......
...@@ -33,7 +33,7 @@ public function getLanguage($type) { ...@@ -33,7 +33,7 @@ public function getLanguage($type) {
// @todo Objectify the language system so that we don't have to do this. // @todo Objectify the language system so that we don't have to do this.
include_once DRUPAL_ROOT . '/core/includes/language.inc'; 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]; return $this->languages[$type];
} }
......
...@@ -200,7 +200,7 @@ function language_from_session($languages) { ...@@ -200,7 +200,7 @@ function language_from_session($languages) {
* @return * @return
* A valid language code on success, FALSE otherwise. * 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; $language_url = FALSE;
if (!language_negotiation_method_enabled(LANGUAGE_NEGOTIATION_URL)) { if (!language_negotiation_method_enabled(LANGUAGE_NEGOTIATION_URL)) {
...@@ -212,9 +212,7 @@ function language_from_url($languages, $args = array()) { ...@@ -212,9 +212,7 @@ function language_from_url($languages, $args = array()) {
// Language negotiation happens before the public function current_path() // Language negotiation happens before the public function current_path()
// is available. // is available.
// @todo Refactor with Symfony's Request object. // @todo Refactor with Symfony's Request object.
if (isset($args['request'])) { if ($request) {
$request = $args['request'];
$current_path = $request->attributes->get('system_path'); $current_path = $request->attributes->get('system_path');
if (!isset($current_path)) { if (!isset($current_path)) {
$current_path = trim($request->getPathInfo(), '/'); $current_path = trim($request->getPathInfo(), '/');
...@@ -285,6 +283,10 @@ function language_from_url($languages, $args = array()) { ...@@ -285,6 +283,10 @@ function language_from_url($languages, $args = array()) {
* (optional) An array of valid language objects. This is passed by * (optional) An array of valid language objects. This is passed by
* language_negotiation_method_invoke() to every language method callback, * language_negotiation_method_invoke() to every language method callback,
* but it is not actually needed here. Defaults to NULL. * but it is not actually needed here. Defaults to NULL.
*
* @param $request
* (optional) The HttpRequest object representing the current request.
*
* @param $language_type * @param $language_type
* (optional) The language type to fall back to. Defaults to the interface * (optional) The language type to fall back to. Defaults to the interface
* language. * language.
...@@ -292,7 +294,7 @@ function language_from_url($languages, $args = array()) { ...@@ -292,7 +294,7 @@ function language_from_url($languages, $args = array()) {
* @return * @return
* A valid language code. * 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(); $default = language_default();
$prefix = (variable_get('language_negotiation_url_part', LANGUAGE_NEGOTIATION_URL_PREFIX) == LANGUAGE_NEGOTIATION_URL_PREFIX); $prefix = (variable_get('language_negotiation_url_part', LANGUAGE_NEGOTIATION_URL_PREFIX) == LANGUAGE_NEGOTIATION_URL_PREFIX);
......
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