You are here

class DomainLangConfigRedirect in Domain Lang 8

Redirect subscriber for control language detection and selection pages.

Hierarchy

Expanded class hierarchy of DomainLangConfigRedirect

1 string reference to 'DomainLangConfigRedirect'
domain_lang.services.yml in ./domain_lang.services.yml
domain_lang.services.yml
1 service uses DomainLangConfigRedirect
domain_lang.config_redirect in ./domain_lang.services.yml
Drupal\domain_lang\EventSubscriber\DomainLangConfigRedirect

File

src/EventSubscriber/DomainLangConfigRedirect.php, line 19

Namespace

Drupal\domain_lang\EventSubscriber
View source
class DomainLangConfigRedirect implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * The domain negotiator.
   *
   * @var \Drupal\domain\DomainNegotiatorInterface
   */
  protected $domainNegotiator;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * Constructs a new class object.
   *
   * @param \Drupal\domain\DomainNegotiatorInterface $domain_negotiator
   *   The domain negotiator service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger.
   */
  public function __construct(DomainNegotiatorInterface $domain_negotiator, MessengerInterface $messenger) {
    $this->domainNegotiator = $domain_negotiator;
    $this->messenger = $messenger;
  }

  /**
   * Check current request and redirect if needed.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   Current request response event.
   */
  public function checkRequest(GetResponseEvent $event) {
    $domain = $this->domainNegotiator
      ->getActiveDomain();
    if ($domain instanceof DomainInterface) {
      switch ($event
        ->getRequest()
        ->get(RouteObjectInterface::ROUTE_NAME)) {
        case 'language.negotiation':
          $this->messenger
            ->addStatus($this
            ->t('You was redirected to current active domain language settings page.'));
          $this->messenger
            ->addStatus($this
            ->t('This page should be used for currently active domain language detection and selection setup.'));
          $this
            ->setRedirectResponse($event, 'domain_lang.admin', $domain);
          break;
        case 'language.negotiation_session':
          $args = [
            '@type' => $this
              ->t('Session language detection configuration'),
          ];
          $this->messenger
            ->addStatus($this
            ->t('You was redirected to current active domain @type page.', $args));
          $this->messenger
            ->addStatus($this
            ->t('This page should be used for currently active domain @type.', $args));
          $this
            ->setRedirectResponse($event, 'domain_lang.negotiation_session', $domain);
          break;
        case 'language.negotiation_browser':
          $args = [
            '@type' => $this
              ->t('Browser language detection configuration'),
          ];
          $this->messenger
            ->addStatus($this
            ->t('You was redirected to current active domain @type page.', $args));
          $this->messenger
            ->addStatus($this
            ->t('This page should be used for currently active domain @type.', $args));
          $this
            ->setRedirectResponse($event, 'domain_lang.negotiation_browser', $domain);
          break;
        case 'language.negotiation_url':
          $args = [
            '@type' => $this
              ->t('URL language detection configuration'),
          ];
          $this->messenger
            ->addStatus($this
            ->t('You was redirected to current active domain @type page.', $args));
          $this->messenger
            ->addStatus($this
            ->t('This page should be used for currently active domain @type.', $args));
          $this
            ->setRedirectResponse($event, 'domain_lang.negotiation_url', $domain);
          break;
        case 'language.negotiation_selected':
          $args = [
            '@type' => $this
              ->t('Selected language configuration'),
          ];
          $this->messenger
            ->addStatus($this
            ->t('You was redirected to current active domain @type page.', $args));
          $this->messenger
            ->addStatus($this
            ->t('This page should be used for currently active domain @type.', $args));
          $this
            ->setRedirectResponse($event, 'domain_lang.negotiation_selected', $domain);
          break;
      }
    }
  }

  /**
   * Sets TrustedRedirectResponse to redirect to related domain page.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The request response event.
   * @param string $route
   *   The name of the route.
   * @param \Drupal\domain\DomainInterface $domain
   *   The domain object.
   */
  protected function setRedirectResponse(GetResponseEvent $event, $route, DomainInterface $domain) {
    $event
      ->setResponse(new TrustedRedirectResponse(Url::fromRoute($route, [
      'domain' => $domain
        ->id(),
    ], [
      'absolute' => TRUE,
    ])
      ->toString()));
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = [
      'checkRequest',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DomainLangConfigRedirect::$domainNegotiator protected property The domain negotiator.
DomainLangConfigRedirect::$messenger protected property The messenger.
DomainLangConfigRedirect::checkRequest public function Check current request and redirect if needed.
DomainLangConfigRedirect::getSubscribedEvents public static function
DomainLangConfigRedirect::setRedirectResponse protected function Sets TrustedRedirectResponse to redirect to related domain page.
DomainLangConfigRedirect::__construct public function Constructs a new class object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.