LanguageNegotiationSession.php in Zircon Profile 8.0
File
core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php
View source
<?php
namespace Drupal\language\Plugin\LanguageNegotiation;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
use Drupal\language\LanguageNegotiationMethodBase;
use Drupal\language\LanguageSwitcherInterface;
use Symfony\Component\HttpFoundation\Request;
class LanguageNegotiationSession extends LanguageNegotiationMethodBase implements OutboundPathProcessorInterface, LanguageSwitcherInterface {
protected $queryRewrite;
protected $queryParam;
protected $queryValue;
const METHOD_ID = 'language-session';
public function getLangcode(Request $request = NULL) {
$config = $this->config
->get('language.negotiation')
->get('session');
$param = $config['parameter'];
$langcode = $request && $request->query
->get($param) ? $request->query
->get($param) : NULL;
if (!$langcode && isset($_SESSION[$param])) {
$langcode = $_SESSION[$param];
}
return $langcode;
}
public function persist(LanguageInterface $language) {
$langcode = $language
->getId();
if ($langcode && $this->languageManager) {
$languages = $this->languageManager
->getLanguages();
if ($this->currentUser
->isAuthenticated() && isset($languages[$langcode])) {
$config = $this->config
->get('language.negotiation')
->get('session');
$_SESSION[$config['parameter']] = $langcode;
}
}
}
public function processOutbound($path, &$options = array(), Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
if ($request) {
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 ($this->queryRewrite) {
if (isset($options['query']) && is_string($options['query'])) {
$query = array();
parse_str($options['query'], $query);
$options['query'] = $query;
}
if (!isset($options['query'][$this->queryParam])) {
$options['query'][$this->queryParam] = $this->queryValue;
}
if ($bubbleable_metadata) {
$bubbleable_metadata
->addCacheTags($this->config
->get('language.negotiation')
->getCacheTags())
->addCacheContexts([
'url.query_args:' . $this->queryParam,
]);
}
}
}
return $path;
}
public function getLanguageSwitchLinks(Request $request, $type, Url $url) {
$links = array();
$config = $this->config
->get('language.negotiation')
->get('session');
$param = $config['parameter'];
$language_query = isset($_SESSION[$param]) ? $_SESSION[$param] : $this->languageManager
->getCurrentLanguage($type)
->getId();
$query = array();
parse_str($request
->getQueryString(), $query);
foreach ($this->languageManager
->getNativeLanguages() as $language) {
$langcode = $language
->getId();
$links[$langcode] = array(
'url' => clone $url,
'title' => $language
->getName(),
'attributes' => array(
'class' => array(
'language-link',
),
),
'query' => $query,
);
if ($language_query != $langcode) {
$links[$langcode]['query'][$param] = $langcode;
}
else {
$links[$langcode]['attributes']['class'][] = 'session-active';
}
}
return $links;
}
}