You are here

public function LanguageNegotiationSession::processOutbound in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSession::processOutbound()
  2. 10 core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSession::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/LanguageNegotiationSession.php, line 84

Class

LanguageNegotiationSession
Identify language from a request/session parameter.

Namespace

Drupal\language\Plugin\LanguageNegotiation

Code

public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
  if ($request) {

    // The following values are not supposed to change during a single page
    // request processing.
    if (!isset($this->queryRewrite)) {
      if ($this->currentUser
        ->isAnonymous()) {
        $languages = $this->languageManager
          ->getLanguages();
        $config = $this->config
          ->get('language.negotiation')
          ->get('session');
        $this->queryParam = $config['parameter'];
        $this->queryValue = $request->query
          ->has($this->queryParam) ? $request->query
          ->get($this->queryParam) : NULL;
        $this->queryRewrite = isset($languages[$this->queryValue]);
      }
      else {
        $this->queryRewrite = FALSE;
      }
    }

    // If the user is anonymous, the user language negotiation method is
    // enabled, and the corresponding option has been set, we must preserve
    // any explicit user language preference even with cookies disabled.
    if ($this->queryRewrite) {
      if (!isset($options['query'][$this->queryParam])) {
        $options['query'][$this->queryParam] = $this->queryValue;
      }
      if ($bubbleable_metadata) {

        // Cached URLs that have been processed by this outbound path
        // processor must be:
        $bubbleable_metadata
          ->addCacheTags($this->config
          ->get('language.negotiation')
          ->getCacheTags())
          ->addCacheContexts([
          'url.query_args:' . $this->queryParam,
        ]);
      }
    }
  }
  return $path;
}