You are here

final class LanguageSelectionPageSubscriber in Language Selection Page 8.2

Provides a LanguageSelectionPageSubscriber.

Hierarchy

  • class \Drupal\language_selection_page\EventSubscriber\LanguageSelectionPageSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of LanguageSelectionPageSubscriber

1 string reference to 'LanguageSelectionPageSubscriber'
language_selection_page.services.yml in ./language_selection_page.services.yml
language_selection_page.services.yml
1 service uses LanguageSelectionPageSubscriber
language_selection_page.language_selection_page_subscriber in ./language_selection_page.services.yml
Drupal\language_selection_page\EventSubscriber\LanguageSelectionPageSubscriber

File

src/EventSubscriber/LanguageSelectionPageSubscriber.php, line 23

Namespace

Drupal\language_selection_page\EventSubscriber
View source
final class LanguageSelectionPageSubscriber implements EventSubscriberInterface {

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  private $configFactory;

  /**
   * The current path.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  private $currentPath;

  /**
   * The event.
   *
   * @var \Symfony\Component\HttpKernel\Event\FilterResponseEvent
   */
  private $event;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  private $languageManager;

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

  /**
   * The Language Selection Page condition plugin manager.
   *
   * @var \Drupal\Core\Executable\ExecutableManagerInterface
   */
  private $languageSelectionPageConditionManager;

  /**
   * Constructs a new class object.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Drupal\language\LanguageNegotiatorInterface $language_negotiator
   *   The language negotiator.
   * @param \Drupal\Core\Path\CurrentPathStack $current_path
   *   The current path.
   * @param \Drupal\Core\Executable\ExecutableManagerInterface $plugin_manager
   *   The language selection page condition plugin manager.
   */
  public function __construct(LanguageManagerInterface $language_manager, ConfigFactoryInterface $config_factory, LanguageNegotiatorInterface $language_negotiator, CurrentPathStack $current_path, ExecutableManagerInterface $plugin_manager) {
    $this->languageManager = $language_manager;
    $this->configFactory = $config_factory;
    $this->languageNegotiator = $language_negotiator;
    $this->currentPath = $current_path;
    $this->languageSelectionPageConditionManager = $plugin_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // You can set the order of execution of this event callback in the array.
    // Find the order of execution by doing this in the Drupal Root:
    // grep "$events[KernelEvents::RESPONSE][]" . -R | grep -v 'Test'
    // The value is currently set to -50, feel free to adjust if needed.
    $events[KernelEvents::RESPONSE][] = [
      'redirectToLanguageSelectionPage',
      -50,
    ];
    return $events;
  }

  /**
   * Event callback.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event object.
   *
   * @return bool
   *   Returns FALSE.
   */
  public function redirectToLanguageSelectionPage(FilterResponseEvent $event) {
    $this->event = $event;
    $config = $this->configFactory
      ->get('language_selection_page.negotiation');
    $manager = $this->languageSelectionPageConditionManager;
    foreach ($manager
      ->getDefinitions() as $def) {

      /** @var ExecutableInterface $condition_plugin */
      $condition_plugin = $manager
        ->createInstance($def['id'], $config
        ->get());
      if (!$manager
        ->execute($condition_plugin)) {
        return FALSE;
      }
    }
    if (!$this
      ->getLanguage()) {
      $request = $this->event
        ->getRequest();
      $url = sprintf('%s?destination=%s', $request
        ->getBasePath() . $config
        ->get('path'), $this->currentPath
        ->getPath($request));
      $response = new RedirectResponse($url);
      $event
        ->setResponse($response);
    }
    return FALSE;
  }

  /**
   * Callback helper.
   *
   * @return array|bool
   *   The language to use, or FALSE.
   */
  private function getLanguage() {

    // Get all methods available for the user interface language type.
    $methods = $this->configFactory
      ->get('language.types')
      ->get('negotiation.' . LanguageInterface::TYPE_INTERFACE . '.enabled') ?: [];

    // We ignore this language method or else it will always return a language.
    unset($methods[LanguageNegotiationSelected::METHOD_ID]);
    foreach ($methods as $method_id => $method_definition) {

      // Do not consider methods with a lower priority than the language
      // selection page method, nor the language selection page method itself.
      if ($method_id === LanguageNegotiationLanguageSelectionPage::METHOD_ID) {
        return FALSE;
      }
      $lang = $this->languageNegotiator
        ->getNegotiationMethodInstance($method_id)
        ->getLangcode($this->event
        ->getRequest());
      if ($lang) {
        return $lang;
      }
    }

    // If no other language was found, use the default one.
    return $this->languageManager
      ->getDefaultLanguage()
      ->getId();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LanguageSelectionPageSubscriber::$configFactory private property The configuration factory.
LanguageSelectionPageSubscriber::$currentPath private property The current path.
LanguageSelectionPageSubscriber::$event private property The event.
LanguageSelectionPageSubscriber::$languageManager private property The language manager.
LanguageSelectionPageSubscriber::$languageNegotiator private property The language negotiator.
LanguageSelectionPageSubscriber::$languageSelectionPageConditionManager private property The Language Selection Page condition plugin manager.
LanguageSelectionPageSubscriber::getLanguage private function Callback helper.
LanguageSelectionPageSubscriber::getSubscribedEvents public static function
LanguageSelectionPageSubscriber::redirectToLanguageSelectionPage public function Event callback.
LanguageSelectionPageSubscriber::__construct public function Constructs a new class object.