You are here

class LanguageRequestSubscriber in Drupal 10

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

Sets the $request property on the language manager.

Hierarchy

  • class \Drupal\language\EventSubscriber\LanguageRequestSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of LanguageRequestSubscriber

File

core/modules/language/src/EventSubscriber/LanguageRequestSubscriber.php, line 17

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.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Constructs a LanguageRequestSubscriber object.
   *
   * @param \Drupal\language\ConfigurableLanguageManagerInterface $language_manager
   *   The language manager service.
   * @param \Drupal\language\LanguageNegotiatorInterface $negotiator
   *   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;
  }

  /**
   * Initializes the language manager at the beginning of the request.
   *
   * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
   *   The Event to process.
   */
  public function onKernelRequestLanguage(RequestEvent $event) {
    if ($event
      ->isMainRequest()) {
      $this
        ->setLanguageOverrides();
    }
  }

  /**
   * 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);
  }

  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  public static function getSubscribedEvents() : array {
    $events[KernelEvents::REQUEST][] = [
      'onKernelRequestLanguage',
      255,
    ];
    $events[DrupalKernelInterface::CONTAINER_INITIALIZE_SUBREQUEST_FINISHED][] = [
      'onContainerInitializeSubrequestFinished',
      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 public static function Registers the methods in this class that should be listeners.
LanguageRequestSubscriber::onContainerInitializeSubrequestFinished public function Initializes config overrides whenever the service container is rebuilt.
LanguageRequestSubscriber::onKernelRequestLanguage public function Initializes the language manager at the beginning of the request.
LanguageRequestSubscriber::setLanguageOverrides private function Sets the language for config overrides on the language manager.
LanguageRequestSubscriber::__construct public function Constructs a LanguageRequestSubscriber object.