LanguageRequestSubscriber.php 3.67 KB
Newer Older
1 2
<?php

3
namespace Drupal\language\EventSubscriber;
4

5
use Drupal\Core\DrupalKernelInterface;
6
use Drupal\Core\Session\AccountInterface;
7
use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
8 9
use Drupal\language\ConfigurableLanguageManagerInterface;
use Drupal\language\LanguageNegotiatorInterface;
10 11 12 13 14 15 16 17 18 19 20 21 22
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Sets the $request property on the language manager.
 */
class LanguageRequestSubscriber implements EventSubscriberInterface {

  /**
   * The language manager service.
   *
23
   * @var \Drupal\language\ConfigurableLanguageManagerInterface
24 25 26
   */
  protected $languageManager;

27 28 29 30 31 32 33
  /**
   * The language negotiator.
   *
   * @var \Drupal\language\LanguageNegotiatorInterface
   */
  protected $negotiator;

34 35 36
  /**
   * The translation service.
   *
37
   * @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface
38 39 40
   */
  protected $translation;

41 42 43
  /**
   * The current active user.
   *
44
   * @var \Drupal\Core\Session\AccountInterface
45 46 47
   */
  protected $currentUser;

48 49 50
  /**
   * Constructs a LanguageRequestSubscriber object.
   *
51
   * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
52
   *   The language manager service.
53
   * @param \Drupal\language\LanguageNegotiatorInterface $negotiator
54
   *   The language negotiator.
55
   * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translation
56
   *   The translation service.
57 58
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current active user.
59
   */
60
  public function __construct(ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, TranslatorInterface $translation, AccountInterface $current_user) {
61
    $this->languageManager = $language_manager;
62
    $this->negotiator = $negotiator;
63
    $this->translation = $translation;
64
    $this->currentUser = $current_user;
65 66 67
  }

  /**
68
   * Initializes the language manager at the beginning of the request.
69 70 71 72 73 74
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The Event to process.
   */
  public function onKernelRequestLanguage(GetResponseEvent $event) {
    if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
75
      $this->setLanguageOverrides();
76 77 78
    }
  }

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  /**
   * Initializes config overrides whenever the service container is rebuilt.
   */
  public function onContainerInitializeSubrequestFinished() {
    $this->setLanguageOverrides();
  }

  /**
   * Sets the language for config overrides on the language manager.
   */
  private function setLanguageOverrides() {
    $this->negotiator->setCurrentUser($this->currentUser);
    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);
  }

101 102 103 104 105 106
  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
107
  public static function getSubscribedEvents() {
108
    $events[KernelEvents::REQUEST][] = ['onKernelRequestLanguage', 255];
109
    $events[DrupalKernelInterface::CONTAINER_INITIALIZE_SUBREQUEST_FINISHED][] = ['onContainerInitializeSubrequestFinished', 255];
110 111 112 113 114

    return $events;
  }

}