You are here

public function LanguageNegotiationUrl::processOutbound in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::processOutbound()
  2. 10 core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::processOutbound()

Processes the outbound path.

Parameters

string $path: The path to process, with a leading slash.

array $options: (optional) An associative array of additional options, with the following elements:

  • 'query': An array of query key/value-pairs (without any URL-encoding) to append to the URL.
  • 'fragment': A fragment identifier (named anchor) to append to the URL. Do not include the leading '#' character.
  • 'absolute': Defaults to FALSE. Whether to force the output to be an absolute link (beginning with http:). Useful for links that will be displayed outside the site, such as in an RSS feed.
  • 'language': An optional language object used to look up the alias for the URL. If $options['language'] is omitted, it defaults to the current language for the language type LanguageInterface::TYPE_URL.
  • 'https': Whether this URL should point to a secure location. If not defined, the current scheme is used, so the user stays on HTTP or HTTPS respectively. TRUE enforces HTTPS and FALSE enforces HTTP.
  • 'base_url': Only used internally by a path processor, for example, to modify the base URL when a language dependent URL requires so.
  • 'prefix': Only used internally, to modify the path when a language dependent URL requires so.
  • 'route': The route object for the given path. It will be set by \Drupal\Core\Routing\UrlGenerator::generateFromRoute().

\Symfony\Component\HttpFoundation\Request $request: The HttpRequest object representing the current request.

\Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata: (optional) Object to collect path processors' bubbleable metadata.

Return value

string The processed path.

Overrides OutboundPathProcessorInterface::processOutbound

File

core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationUrl.php, line 124

Class

LanguageNegotiationUrl
Class for identifying language via URL prefix or domain.

Namespace

Drupal\language\Plugin\LanguageNegotiation

Code

public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
  $url_scheme = 'http';
  $port = 80;
  if ($request) {
    $url_scheme = $request
      ->getScheme();
    $port = $request
      ->getPort();
  }
  $languages = array_flip(array_keys($this->languageManager
    ->getLanguages()));

  // Language can be passed as an option, or we go for current URL language.
  if (!isset($options['language'])) {
    $language_url = $this->languageManager
      ->getCurrentLanguage(LanguageInterface::TYPE_URL);
    $options['language'] = $language_url;
  }
  elseif (!is_object($options['language']) || !isset($languages[$options['language']
    ->getId()])) {
    return $path;
  }
  $config = $this->config
    ->get('language.negotiation')
    ->get('url');
  if ($config['source'] == LanguageNegotiationUrl::CONFIG_PATH_PREFIX) {
    if (is_object($options['language']) && !empty($config['prefixes'][$options['language']
      ->getId()])) {
      $options['prefix'] = $config['prefixes'][$options['language']
        ->getId()] . '/';
      if ($bubbleable_metadata) {
        $bubbleable_metadata
          ->addCacheContexts([
          'languages:' . LanguageInterface::TYPE_URL,
        ]);
      }
    }
  }
  elseif ($config['source'] == LanguageNegotiationUrl::CONFIG_DOMAIN) {
    if (is_object($options['language']) && !empty($config['domains'][$options['language']
      ->getId()])) {

      // 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([
          'https://',
          'http://',
        ], '', $options['base_url']);
      }

      // Ask for an absolute URL with our modified base URL.
      $options['absolute'] = TRUE;
      $options['base_url'] = $url_scheme . '://' . $config['domains'][$options['language']
        ->getId()];

      // In case either the original base URL or the HTTP host contains a
      // port, retain it.
      if (isset($normalized_base_url) && strpos($normalized_base_url, ':') !== FALSE) {
        list(, $port) = explode(':', $normalized_base_url);
        $options['base_url'] .= ':' . $port;
      }
      elseif ($url_scheme == 'http' && $port != 80 || $url_scheme == 'https' && $port != 443) {
        $options['base_url'] .= ':' . $port;
      }
      if (isset($options['https'])) {
        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']);
        }
      }

      // Add Drupal's subfolder from the base_path if there is one.
      $options['base_url'] .= rtrim(base_path(), '/');
      if ($bubbleable_metadata) {
        $bubbleable_metadata
          ->addCacheContexts([
          'languages:' . LanguageInterface::TYPE_URL,
          'url.site',
        ]);
      }
    }
  }
  return $path;
}