function LanguageNegotiationSession::processOutbound

Same name and namespace in other branches
  1. 9 core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSession::processOutbound()
  2. 8.9.x core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSession::processOutbound()
  3. 10 core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSession::processOutbound()

Overrides OutboundPathProcessorInterface::processOutbound

File

core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php, line 117

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.