diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index ba2c0fb..1b23afd 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -30,6 +30,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; +use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\TerminableInterface; use Symfony\Component\Routing\Route; @@ -915,6 +916,7 @@ protected function initializeContainer() { if ($this->containerNeedsDumping && !$this->cacheDrupalContainer($container_definition)) { $this->container->get('logger.factory')->get('DrupalKernel')->error('Container cannot be saved to cache.'); } + $this->container->get('event_dispatcher')->dispatch(KernelEvents::CONTAINER_INITIALIZE_FINISHED); return $this->container; } diff --git a/core/lib/Drupal/Core/DrupalKernelInterface.php b/core/lib/Drupal/Core/DrupalKernelInterface.php index 862fbec..1c9fe7c 100644 --- a/core/lib/Drupal/Core/DrupalKernelInterface.php +++ b/core/lib/Drupal/Core/DrupalKernelInterface.php @@ -15,6 +15,15 @@ interface DrupalKernelInterface extends HttpKernelInterface, ContainerAwareInterface { /** + * Thde CONTAINER_INITIALIZE_FINISHED event occurs when the service container finished initializing. + * + * This event allows you to initialize overrides such as language to the services. + * + * @var string + */ + const CONTAINER_INITIALIZE_FINISHED = 'kernel.container.finish_container_initialize'; + + /** * Boots the current kernel. * * @return $this diff --git a/core/modules/language/src/EventSubscriber/LanguageRequestSubscriber.php b/core/modules/language/src/EventSubscriber/LanguageRequestSubscriber.php index c805b0f..00727d5 100644 --- a/core/modules/language/src/EventSubscriber/LanguageRequestSubscriber.php +++ b/core/modules/language/src/EventSubscriber/LanguageRequestSubscriber.php @@ -2,6 +2,7 @@ namespace Drupal\language\EventSubscriber; +use Drupal\Core\DrupalKernelInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\Translator\TranslatorInterface; use Drupal\language\ConfigurableLanguageManagerInterface; @@ -64,28 +65,41 @@ public function __construct(ConfigurableLanguageManagerInterface $language_manag } /** - * Sets the request on the language manager. + * Sets the request on the language manager at the beginning of the request. * * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event * The Event to process. */ public function onKernelRequestLanguage(GetResponseEvent $event) { if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { - $request = $event->getRequest(); - $this->negotiator->setCurrentUser($this->currentUser); - $this->negotiator->reset(); - if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) { - $this->languageManager->setNegotiator($this->negotiator); - $this->languageManager->setConfigOverrideLanguage($this->languageManager->getCurrentLanguage()); - } - // After the language manager has initialized, set the default langcode - // for the string translations. - $langcode = $this->languageManager->getCurrentLanguage()->getId(); - $this->translation->setDefaultLangcode($langcode); + $this->setLanguageOverrides(); } } /** + * Sets the request on the language manager whenever the service container is rebuilt. + */ + public function onContainerInitializeFinished() { + $this->setLanguageOverrides(); + } + + /** + * Sets the request on the language manager. + */ + private function setLanguageOverrides() { + $this->negotiator->setCurrentUser($this->currentUser); + $this->negotiator->reset(); + if ($this->languageManager instanceof ConfigurableLanguageManagerInterface) { + $this->languageManager->setNegotiator($this->negotiator); + $this->languageManager->setConfigOverrideLanguage($this->languageManager->getCurrentLanguage()); + } + // After the language manager has initialized, set the default langcode + // for the string translations. + $langcode = $this->languageManager->getCurrentLanguage()->getId(); + $this->translation->setDefaultLangcode($langcode); + } + + /** * Registers the methods in this class that should be listeners. * * @return array @@ -93,6 +107,7 @@ public function onKernelRequestLanguage(GetResponseEvent $event) { */ static function getSubscribedEvents() { $events[KernelEvents::REQUEST][] = array('onKernelRequestLanguage', 255); + $events[DrupalKernelInterface::CONTAINER_INITIALIZE_FINISHED][] = array('onContainerInitializeFinished', 255); return $events; }