annotate core/modules/language/src/EventSubscriber/LanguageRequestSubscriber.php @ 19:fa3358dc1485 tip

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