Chris@0: config->get('language.negotiation')->get('session'); Chris@0: $param = $config['parameter']; Chris@0: $langcode = $request && $request->query->get($param) ? $request->query->get($param) : NULL; Chris@0: if (!$langcode && isset($_SESSION[$param])) { Chris@0: $langcode = $_SESSION[$param]; Chris@0: } Chris@0: return $langcode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function persist(LanguageInterface $language) { Chris@0: // We need to update the session parameter with the request value only if we Chris@0: // have an authenticated user. Chris@0: $langcode = $language->getId(); Chris@0: if ($langcode && $this->languageManager) { Chris@0: $languages = $this->languageManager->getLanguages(); Chris@0: if ($this->currentUser->isAuthenticated() && isset($languages[$langcode])) { Chris@0: $config = $this->config->get('language.negotiation')->get('session'); Chris@0: $_SESSION[$config['parameter']] = $langcode; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) { Chris@0: if ($request) { Chris@0: // The following values are not supposed to change during a single page Chris@0: // request processing. Chris@0: if (!isset($this->queryRewrite)) { Chris@0: if ($this->currentUser->isAnonymous()) { Chris@0: $languages = $this->languageManager->getLanguages(); Chris@0: $config = $this->config->get('language.negotiation')->get('session'); Chris@0: $this->queryParam = $config['parameter']; Chris@0: $this->queryValue = $request->query->has($this->queryParam) ? $request->query->get($this->queryParam) : NULL; Chris@0: $this->queryRewrite = isset($languages[$this->queryValue]); Chris@0: } Chris@0: else { Chris@0: $this->queryRewrite = FALSE; Chris@0: } Chris@0: } Chris@0: Chris@0: // If the user is anonymous, the user language negotiation method is Chris@0: // enabled, and the corresponding option has been set, we must preserve Chris@0: // any explicit user language preference even with cookies disabled. Chris@0: if ($this->queryRewrite) { Chris@0: if (!isset($options['query'][$this->queryParam])) { Chris@0: $options['query'][$this->queryParam] = $this->queryValue; Chris@0: } Chris@0: if ($bubbleable_metadata) { Chris@0: // Cached URLs that have been processed by this outbound path Chris@0: // processor must be: Chris@0: $bubbleable_metadata Chris@0: // - invalidated when the language negotiation config changes, since Chris@0: // another query parameter may be used to determine the language. Chris@0: ->addCacheTags($this->config->get('language.negotiation')->getCacheTags()) Chris@0: // - varied by the configured query parameter. Chris@0: ->addCacheContexts(['url.query_args:' . $this->queryParam]); Chris@0: } Chris@0: } Chris@0: } Chris@0: return $path; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getLanguageSwitchLinks(Request $request, $type, Url $url) { Chris@0: $links = []; Chris@0: $config = $this->config->get('language.negotiation')->get('session'); Chris@0: $param = $config['parameter']; Chris@0: $language_query = isset($_SESSION[$param]) ? $_SESSION[$param] : $this->languageManager->getCurrentLanguage($type)->getId(); Chris@0: $query = []; Chris@0: parse_str($request->getQueryString(), $query); Chris@0: Chris@0: foreach ($this->languageManager->getNativeLanguages() as $language) { Chris@0: $langcode = $language->getId(); Chris@0: $links[$langcode] = [ Chris@0: // We need to clone the $url object to avoid using the same one for all Chris@0: // links. When the links are rendered, options are set on the $url Chris@0: // object, so if we use the same one, they would be set for all links. Chris@0: 'url' => clone $url, Chris@0: 'title' => $language->getName(), Chris@0: 'attributes' => ['class' => ['language-link']], Chris@0: 'query' => $query, Chris@0: ]; Chris@0: if ($language_query != $langcode) { Chris@0: $links[$langcode]['query'][$param] = $langcode; Chris@0: } Chris@0: else { Chris@0: $links[$langcode]['attributes']['class'][] = 'session-active'; Chris@0: } Chris@0: } Chris@0: Chris@0: return $links; Chris@0: } Chris@0: Chris@0: }