Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\language\EventSubscriber;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\DrupalKernelInterface;
|
Chris@0
|
6 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
7 use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
|
Chris@0
|
8 use Drupal\language\ConfigurableLanguageManagerInterface;
|
Chris@0
|
9 use Drupal\language\LanguageNegotiatorInterface;
|
Chris@0
|
10 use Symfony\Component\HttpKernel\HttpKernelInterface;
|
Chris@0
|
11 use Symfony\Component\HttpKernel\KernelEvents;
|
Chris@0
|
12 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
Chris@0
|
13 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Sets the $request property on the language manager.
|
Chris@0
|
17 */
|
Chris@0
|
18 class LanguageRequestSubscriber implements EventSubscriberInterface {
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The language manager service.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var \Drupal\language\ConfigurableLanguageManagerInterface
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $languageManager;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The language negotiator.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Drupal\language\LanguageNegotiatorInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 protected $negotiator;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The translation service.
|
Chris@0
|
36 *
|
Chris@0
|
37 * @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface;
|
Chris@0
|
38 */
|
Chris@0
|
39 protected $translation;
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * The current active user.
|
Chris@0
|
43 *
|
Chris@0
|
44 * @return \Drupal\Core\Session\AccountInterface
|
Chris@0
|
45 */
|
Chris@0
|
46 protected $currentUser;
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * Constructs a LanguageRequestSubscriber object.
|
Chris@0
|
50 *
|
Chris@0
|
51 * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
|
Chris@0
|
52 * The language manager service.
|
Chris@0
|
53 * @param \Drupal\language\LanguageNegotiatorInterface $negotiator
|
Chris@0
|
54 * The language negotiator.
|
Chris@0
|
55 * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translation
|
Chris@0
|
56 * The translation service.
|
Chris@0
|
57 * @param \Drupal\Core\Session\AccountInterface $current_user
|
Chris@0
|
58 * The current active user.
|
Chris@0
|
59 */
|
Chris@0
|
60 public function __construct(ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, TranslatorInterface $translation, AccountInterface $current_user) {
|
Chris@0
|
61 $this->languageManager = $language_manager;
|
Chris@0
|
62 $this->negotiator = $negotiator;
|
Chris@0
|
63 $this->translation = $translation;
|
Chris@0
|
64 $this->currentUser = $current_user;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * Initializes the language manager at the beginning of the request.
|
Chris@0
|
69 *
|
Chris@0
|
70 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
|
Chris@0
|
71 * The Event to process.
|
Chris@0
|
72 */
|
Chris@0
|
73 public function onKernelRequestLanguage(GetResponseEvent $event) {
|
Chris@0
|
74 if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
|
Chris@0
|
75 $this->setLanguageOverrides();
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * Initializes config overrides whenever the service container is rebuilt.
|
Chris@0
|
81 */
|
Chris@0
|
82 public function onContainerInitializeSubrequestFinished() {
|
Chris@0
|
83 $this->setLanguageOverrides();
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * Sets the language for config overrides on the language manager.
|
Chris@0
|
88 */
|
Chris@0
|
89 private function setLanguageOverrides() {
|
Chris@0
|
90 $this->negotiator->setCurrentUser($this->currentUser);
|
Chris@0
|
91 if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) {
|
Chris@0
|
92 $this->languageManager->setNegotiator($this->negotiator);
|
Chris@0
|
93 $this->languageManager->setConfigOverrideLanguage($this->languageManager->getCurrentLanguage());
|
Chris@0
|
94 }
|
Chris@0
|
95 // After the language manager has initialized, set the default langcode for
|
Chris@0
|
96 // the string translations.
|
Chris@0
|
97 $langcode = $this->languageManager->getCurrentLanguage()->getId();
|
Chris@0
|
98 $this->translation->setDefaultLangcode($langcode);
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * Registers the methods in this class that should be listeners.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @return array
|
Chris@0
|
105 * An array of event listener definitions.
|
Chris@0
|
106 */
|
Chris@0
|
107 public static function getSubscribedEvents() {
|
Chris@0
|
108 $events[KernelEvents::REQUEST][] = ['onKernelRequestLanguage', 255];
|
Chris@0
|
109 $events[DrupalKernelInterface::CONTAINER_INITIALIZE_SUBREQUEST_FINISHED][] = ['onContainerInitializeSubrequestFinished', 255];
|
Chris@0
|
110
|
Chris@0
|
111 return $events;
|
Chris@0
|
112 }
|
Chris@0
|
113
|
Chris@0
|
114 }
|