Skip to content
Snippets Groups Projects
Commit b979e8aa authored by Katherine Bailey's avatar Katherine Bailey
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
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -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;
}
......
......@@ -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];
}
......
......@@ -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);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment