You are here

LanguageSelectionPageSubscriber.php in Language Selection Page 8.2

File

src/EventSubscriber/LanguageSelectionPageSubscriber.php
View source
<?php

declare (strict_types=1);
namespace Drupal\language_selection_page\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Executable\ExecutableManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\language\LanguageNegotiatorInterface;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected;
use Drupal\language_selection_page\Plugin\LanguageNegotiation\LanguageNegotiationLanguageSelectionPage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Provides a LanguageSelectionPageSubscriber.
 */
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();
  }

}

Classes

Namesort descending Description
LanguageSelectionPageSubscriber Provides a LanguageSelectionPageSubscriber.