1.20.x locale.inc locale_language_url_rewrite_url(&$path, &$options)

Rewrite URLs for the URL language provider.

Related topics

File

includes/locale.inc, line 415
Administration functions for locale.module.

Code

function locale_language_url_rewrite_url(&$path, &$options) {
  static $backdrop_static_fast;
  if (!isset($backdrop_static_fast)) {
    $backdrop_static_fast['languages'] = &backdrop_static(__FUNCTION__);
  }
  $languages = &$backdrop_static_fast['languages'];

  if (!isset($languages)) {
    // Get the enabled languages only.
    $languages = language_list(TRUE);
    $languages = array_keys($languages);
  }

  // Prevent "undefined index" if array key doesn't even exist.
  if (!isset($options['language'])) {
    $options['language'] = NULL;
  }

  // If language isn't provided as an option, we go for current URL language.
  if (!is_object($options['language'])) {
    global $language_url;
    $options['language'] = $language_url;
  }

  if (is_object($options['language'])) {
    // Sort out disabled languages.
    if (!in_array($options['language']->langcode, $languages)) {
      unset($options['language']);
      return;
    }

    switch (config_get('locale.settings', 'language_negotiation_url_part')) {
      case LANGUAGE_NEGOTIATION_URL_DOMAIN:
        $domains = locale_language_negotiation_url_domains();
        if (!empty($domains[$options['language']->langcode])) {
          // Save the original base URL. If it contains a port, we need to
          // retain it below.
          if (!empty($options['base_url'])) {
            // The colon in the URL scheme messes up the port checking below.
            $normalized_base_url = str_replace(array('https://', 'http://'), '', $options['base_url']);
          }

          // Ask for an absolute URL with our modified base_url.
          global $is_https;
          $url_scheme = ($is_https) ? 'https://' : 'http://';
          $options['absolute'] = TRUE;
          $options['base_url'] = $url_scheme . $domains[$options['language']->langcode];

          // In case either the original base URL or the HTTP host contains a
          // port, retain it.
          $http_host = $_SERVER['HTTP_HOST'];
          if (isset($normalized_base_url) && strpos($normalized_base_url, ':') !== FALSE) {
            list($host, $port) = explode(':', $normalized_base_url);
            $options['base_url'] .= ':' . $port;
          }
          elseif (strpos($http_host, ':') !== FALSE) {
            list($host, $port) = explode(':', $http_host);
            $options['base_url'] .= ':' . $port;
          }

          if (isset($options['https']) && settings_get('https', FALSE)) {
            if ($options['https'] === TRUE) {
              $options['base_url'] = str_replace('http://', 'https://', $options['base_url']);
            }
            elseif ($options['https'] === FALSE) {
              $options['base_url'] = str_replace('https://', 'http://', $options['base_url']);
            }
          }
        }
        break;

      case LANGUAGE_NEGOTIATION_URL_PREFIX:
        $prefixes = locale_language_negotiation_url_prefixes();
        if (!empty($prefixes[$options['language']->langcode])) {
          $options['prefix'] = $prefixes[$options['language']->langcode] . '/';
        }
        break;
    }
  }
}