function language_url_fallback

Determines the language to be assigned to URLs when none is detected.

The language negotiation process has a fallback chain that ends with the default language negotiation method. Each built-in language type has a separate initialization:

  • Interface language, which is the only configurable one, always gets a valid value. If no request-specific language is detected, the default language will be used.
  • Content language merely inherits the interface language by default.
  • URL language is detected from the requested URL and will be used to rewrite URLs appearing in the page being rendered. If no language can be detected, there are two possibilities:

    • If the default language has no configured path prefix or domain, then the default language is used. This guarantees that (missing) URL prefixes are preserved when navigating through the site.
    • If the default language has a configured path prefix or domain, a requested URL having an empty prefix or domain is an anomaly that must be fixed. This is done by introducing a prefix or domain in the rendered page matching the detected interface language.

Parameters

$languages: (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.

$request: (optional) The HttpRequest object representing the current request.

$language_type: (optional) The language type to fall back to. Defaults to the interface language.

Return value

A valid language code.

2 string references to 'language_url_fallback'
language_language_negotiation_info in drupal/core/modules/language/language.module
Implements hook_language_negotiation_info().
locale_update_8007 in drupal/core/modules/locale/locale.install
Convert language_negotiation_* variables to use the new callbacks.

File

drupal/core/modules/language/language.negotiation.inc, line 357
Language negotiation functions.

Code

function language_url_fallback($language = NULL, $request = NULL, $language_type = Language::TYPE_INTERFACE) {
  $default = language_default();
  $prefix = config('language.negotiation')
    ->get('url.source') == LANGUAGE_NEGOTIATION_URL_PREFIX;

  // If the default language is not configured to convey language information,
  // a missing URL language information indicates that URL language should be
  // the default one, otherwise we fall back to an already detected language.
  $domains = language_negotiation_url_domains();
  $prefixes = language_negotiation_url_prefixes();
  if ($prefix && empty($prefixes[$default->langcode]) || !$prefix && empty($domains[$default->langcode])) {
    return $default->langcode;
  }
  else {
    $langcode = language($language_type)->langcode;
    return $langcode;
  }
}