sites_group_overrides-1.x-dev/modules/sites_group_masquerade/src/EventSubscriber/AutoMasqueradeSubscriber.php
modules/sites_group_masquerade/src/EventSubscriber/AutoMasqueradeSubscriber.php
<?php
declare(strict_types=1);
namespace Drupal\sites_group_masquerade\EventSubscriber;
use Drupal\Core\Url;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\group\Entity\GroupInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Drupal\sites_group\SitesGroupServiceInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Drupal\sites_masquerade\SitesMasqueradeInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\EventSubscriber\RedirectResponseSubscriber;
use Drupal\sites\ContextProvider\CurrentSiteContextInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Set the site based on a group parameter.
*/
final class AutoMasqueradeSubscriber implements EventSubscriberInterface {
/**
* Contruscts an AutoMasqueradeSubscriber.
*
* @param \Drupal\sites_group\SitesGroupServiceInterface; $sitesGroupService
* The site group service.
* @param Drupal\sites_masquerade\SitesMasqueradeInterface $sitesMasquerade
* The site masquerade service.
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
* The route match.
* @param \Drupal\sites\ContextProvider\CurrentSiteContextInterface $currentSiteContext
* The current site context.
* @param \Drupal\Core\EventSubscriber\RedirectResponseSubscriber $redirect_response_subscriber
* The redirect response subscriber.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory.
*/
public function __construct(
private readonly SitesGroupServiceInterface $sitesGroupService,
private readonly SitesMasqueradeInterface $sitesMasquerade,
private readonly RouteMatchInterface $routeMatch,
private readonly CurrentSiteContextInterface $currentSiteContext,
private readonly RedirectResponseSubscriber $redirectResponseSubscriber,
private readonly ConfigFactoryInterface $configFactory,
) {}
/**
* Kernel request event handler.
*
* @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
* An event object.
*/
public function onKernelRequest(RequestEvent $event): void {
if (!$this->configFactory->get('sites_group_masquerade.settings')->get('automasquerade')) {
return;
}
// Use the group from URL to set the site.
if ($group = $this->routeMatch->getParameter('group')) {
if (!$group instanceof GroupInterface) {
return;
}
$site = $this->sitesGroupService->getSiteForGroup($group);
$active_site = $this->currentSiteContext->getSiteFromContext();
if (!$active_site || $site->getPluginId() != $active_site->getPluginId()) {
$url = Url::fromRouteMatch($this->routeMatch);
$internal = $url->getInternalPath();
$params = empty($event->getRequest()->query->all()) ? '' : '?' . UrlHelper::buildQuery($event->getRequest()->query->all());
$this->sitesMasquerade->setActiveSite($site);
// Redirect to the internal path - don't apply destination parameter.
$this->redirectResponseSubscriber->setIgnoreDestination(TRUE);
$response = new RedirectResponse('/' . $internal . $params);
$event->setResponse($response);
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
KernelEvents::REQUEST => ['onKernelRequest', 32],
];
}
}
