You are here

class LanguageRequestSubscriber in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/language/src/EventSubscriber/LanguageRequestSubscriber.php \Drupal\language\EventSubscriber\LanguageRequestSubscriber

Sets the $request property on the language manager.

Hierarchy

Expanded class hierarchy of LanguageRequestSubscriber

File

core/modules/language/src/EventSubscriber/LanguageRequestSubscriber.php, line 22
Contains \Drupal\language\EventSubscriber\LanguageRequestSubscriber.

Namespace

Drupal\language\EventSubscriber
View source
class LanguageRequestSubscriber implements EventSubscriberInterface {

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

  /**
   * The language negotiator.
   *
   * @var \Drupal\language\LanguageNegotiatorInterface
   */
  protected $negotiator;

  /**
   * The translation service.
   *
   * @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface;
   */
  protected $translation;

  /**
   * The current active user.
   *
   * @return \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Constructs a LanguageRequestSubscriber object.
   *
   * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
   *   The language manager service.
   * @param \Drupal\language\LanguageNegotiatorInterface
   *   The language negotiator.
   * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $translation;
   *   The translation service.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current active user.
   */
  public function __construct(ConfigurableLanguageManagerInterface $language_manager, LanguageNegotiatorInterface $negotiator, TranslatorInterface $translation, AccountInterface $current_user) {
    $this->languageManager = $language_manager;
    $this->negotiator = $negotiator;
    $this->translation = $translation;
    $this->currentUser = $current_user;
  }

  /**
   * Sets the request on the language manager.
   *
   * @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);
    }
  }

  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array(
      'onKernelRequestLanguage',
      255,
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LanguageRequestSubscriber::$currentUser protected property The current active user.
LanguageRequestSubscriber::$languageManager protected property The language manager service.
LanguageRequestSubscriber::$negotiator protected property The language negotiator.
LanguageRequestSubscriber::$translation protected property The translation service.
LanguageRequestSubscriber::getSubscribedEvents static function Registers the methods in this class that should be listeners. Overrides EventSubscriberInterface::getSubscribedEvents
LanguageRequestSubscriber::onKernelRequestLanguage public function Sets the request on the language manager.
LanguageRequestSubscriber::__construct public function Constructs a LanguageRequestSubscriber object.