annotate core/modules/language/src/Plugin/LanguageNegotiation/LanguageNegotiationSession.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\language\Plugin\LanguageNegotiation;
Chris@0 4
Chris@0 5 use Drupal\Core\Language\LanguageInterface;
Chris@0 6 use Drupal\Core\PathProcessor\OutboundPathProcessorInterface;
Chris@0 7 use Drupal\Core\Render\BubbleableMetadata;
Chris@0 8 use Drupal\Core\Url;
Chris@0 9 use Drupal\language\LanguageNegotiationMethodBase;
Chris@0 10 use Drupal\language\LanguageSwitcherInterface;
Chris@0 11 use Symfony\Component\HttpFoundation\Request;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Identify language from a request/session parameter.
Chris@0 15 *
Chris@0 16 * @LanguageNegotiation(
Chris@0 17 * id = Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSession::METHOD_ID,
Chris@0 18 * weight = -6,
Chris@0 19 * name = @Translation("Session"),
Chris@0 20 * description = @Translation("Language from a request/session parameter."),
Chris@0 21 * config_route_name = "language.negotiation_session"
Chris@0 22 * )
Chris@0 23 */
Chris@0 24 class LanguageNegotiationSession extends LanguageNegotiationMethodBase implements OutboundPathProcessorInterface, LanguageSwitcherInterface {
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Flag used to determine whether query rewriting is active.
Chris@0 28 *
Chris@0 29 * @var bool
Chris@0 30 */
Chris@0 31 protected $queryRewrite;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * The query parameter name to rewrite.
Chris@0 35 *
Chris@0 36 * @var string
Chris@0 37 */
Chris@0 38 protected $queryParam;
Chris@0 39
Chris@0 40 /**
Chris@0 41 * The query parameter value to be set.
Chris@0 42 *
Chris@0 43 * @var string
Chris@0 44 */
Chris@0 45 protected $queryValue;
Chris@0 46
Chris@0 47 /**
Chris@0 48 * The language negotiation method id.
Chris@0 49 */
Chris@0 50 const METHOD_ID = 'language-session';
Chris@0 51
Chris@0 52 /**
Chris@0 53 * {@inheritdoc}
Chris@0 54 */
Chris@0 55 public function getLangcode(Request $request = NULL) {
Chris@0 56 $config = $this->config->get('language.negotiation')->get('session');
Chris@0 57 $param = $config['parameter'];
Chris@0 58 $langcode = $request && $request->query->get($param) ? $request->query->get($param) : NULL;
Chris@0 59 if (!$langcode && isset($_SESSION[$param])) {
Chris@0 60 $langcode = $_SESSION[$param];
Chris@0 61 }
Chris@0 62 return $langcode;
Chris@0 63 }
Chris@0 64
Chris@0 65 /**
Chris@0 66 * {@inheritdoc}
Chris@0 67 */
Chris@0 68 public function persist(LanguageInterface $language) {
Chris@0 69 // We need to update the session parameter with the request value only if we
Chris@0 70 // have an authenticated user.
Chris@0 71 $langcode = $language->getId();
Chris@0 72 if ($langcode && $this->languageManager) {
Chris@0 73 $languages = $this->languageManager->getLanguages();
Chris@0 74 if ($this->currentUser->isAuthenticated() && isset($languages[$langcode])) {
Chris@0 75 $config = $this->config->get('language.negotiation')->get('session');
Chris@0 76 $_SESSION[$config['parameter']] = $langcode;
Chris@0 77 }
Chris@0 78 }
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * {@inheritdoc}
Chris@0 83 */
Chris@0 84 public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
Chris@0 85 if ($request) {
Chris@0 86 // The following values are not supposed to change during a single page
Chris@0 87 // request processing.
Chris@0 88 if (!isset($this->queryRewrite)) {
Chris@0 89 if ($this->currentUser->isAnonymous()) {
Chris@0 90 $languages = $this->languageManager->getLanguages();
Chris@0 91 $config = $this->config->get('language.negotiation')->get('session');
Chris@0 92 $this->queryParam = $config['parameter'];
Chris@0 93 $this->queryValue = $request->query->has($this->queryParam) ? $request->query->get($this->queryParam) : NULL;
Chris@0 94 $this->queryRewrite = isset($languages[$this->queryValue]);
Chris@0 95 }
Chris@0 96 else {
Chris@0 97 $this->queryRewrite = FALSE;
Chris@0 98 }
Chris@0 99 }
Chris@0 100
Chris@0 101 // If the user is anonymous, the user language negotiation method is
Chris@0 102 // enabled, and the corresponding option has been set, we must preserve
Chris@0 103 // any explicit user language preference even with cookies disabled.
Chris@0 104 if ($this->queryRewrite) {
Chris@0 105 if (!isset($options['query'][$this->queryParam])) {
Chris@0 106 $options['query'][$this->queryParam] = $this->queryValue;
Chris@0 107 }
Chris@0 108 if ($bubbleable_metadata) {
Chris@0 109 // Cached URLs that have been processed by this outbound path
Chris@0 110 // processor must be:
Chris@0 111 $bubbleable_metadata
Chris@0 112 // - invalidated when the language negotiation config changes, since
Chris@0 113 // another query parameter may be used to determine the language.
Chris@0 114 ->addCacheTags($this->config->get('language.negotiation')->getCacheTags())
Chris@0 115 // - varied by the configured query parameter.
Chris@0 116 ->addCacheContexts(['url.query_args:' . $this->queryParam]);
Chris@0 117 }
Chris@0 118 }
Chris@0 119 }
Chris@0 120 return $path;
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * {@inheritdoc}
Chris@0 125 */
Chris@0 126 public function getLanguageSwitchLinks(Request $request, $type, Url $url) {
Chris@0 127 $links = [];
Chris@0 128 $config = $this->config->get('language.negotiation')->get('session');
Chris@0 129 $param = $config['parameter'];
Chris@0 130 $language_query = isset($_SESSION[$param]) ? $_SESSION[$param] : $this->languageManager->getCurrentLanguage($type)->getId();
Chris@0 131 $query = [];
Chris@0 132 parse_str($request->getQueryString(), $query);
Chris@0 133
Chris@0 134 foreach ($this->languageManager->getNativeLanguages() as $language) {
Chris@0 135 $langcode = $language->getId();
Chris@0 136 $links[$langcode] = [
Chris@0 137 // We need to clone the $url object to avoid using the same one for all
Chris@0 138 // links. When the links are rendered, options are set on the $url
Chris@0 139 // object, so if we use the same one, they would be set for all links.
Chris@0 140 'url' => clone $url,
Chris@0 141 'title' => $language->getName(),
Chris@0 142 'attributes' => ['class' => ['language-link']],
Chris@0 143 'query' => $query,
Chris@0 144 ];
Chris@0 145 if ($language_query != $langcode) {
Chris@0 146 $links[$langcode]['query'][$param] = $langcode;
Chris@0 147 }
Chris@0 148 else {
Chris@0 149 $links[$langcode]['attributes']['class'][] = 'session-active';
Chris@0 150 }
Chris@0 151 }
Chris@0 152
Chris@0 153 return $links;
Chris@0 154 }
Chris@0 155
Chris@0 156 }