diff --git a/micro_site.info.yml b/micro_site.info.yml index cb0d5b42009ef8ca050bb05c72acd51b4d583854..a001fa7ef725c3682746394245487b6a22ed528f 100644 --- a/micro_site.info.yml +++ b/micro_site.info.yml @@ -1,7 +1,7 @@ name: 'Micro site' type: module description: 'Provide the content entity type site.' -core_version_requirement: ^9.2 || ^10 +core_version_requirement: ^10.1 || ^11 package: 'Micro Site' configure: micro_site.settings dependencies: diff --git a/micro_site.services.yml b/micro_site.services.yml index d8606b4f3c4d906c5b3270c6269fdf96ec8d8c10..a33c28b01a36f04e02730af5f663a6c2bd6e569d 100644 --- a/micro_site.services.yml +++ b/micro_site.services.yml @@ -52,3 +52,9 @@ services: class: \Drupal\micro_site\SiteConfigOverrides tags: - {name: config.factory.override, priority: 100} + + micro_site.request_subscriber: + class: Drupal\micro_site\EventSubscriber\RequestSubscriber + arguments: ['@router.route_provider', '@current_user', '@request_stack'] + tags: + - { name: event_subscriber } diff --git a/src/EventSubscriber/NodeSubscriber.php b/src/EventSubscriber/NodeSubscriber.php index 06b0b5a85d5b2d4b1ed5e3302728dc640097f122..d398a4093964225a0c170bd8bad74b44d714e4e0 100644 --- a/src/EventSubscriber/NodeSubscriber.php +++ b/src/EventSubscriber/NodeSubscriber.php @@ -70,7 +70,7 @@ class NodeSubscriber implements EventSubscriberInterface { /** * {@inheritdoc} */ - public static function getSubscribedEvents() { + public static function getSubscribedEvents(): array { $events[KernelEvents::REQUEST] = array('onRequest'); return $events; } diff --git a/src/EventSubscriber/RequestSubscriber.php b/src/EventSubscriber/RequestSubscriber.php new file mode 100644 index 0000000000000000000000000000000000000000..972d50392fc750ada215d41cd94f7e453fdd7742 --- /dev/null +++ b/src/EventSubscriber/RequestSubscriber.php @@ -0,0 +1,41 @@ +<?php + +namespace Drupal\micro_site\EventSubscriber; + +use Drupal\Core\Routing\CacheableRouteProviderInterface; +use Drupal\Core\Routing\RouteProviderInterface; +use Drupal\Core\Session\AccountInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpKernel\Event\RequestEvent; +use Symfony\Component\HttpKernel\KernelEvents; + +class RequestSubscriber implements EventSubscriberInterface { + public function __construct( + protected RouteProviderInterface $routeProvider, + protected AccountInterface $currentUser, + protected RequestStack $requestStack, + ) { + } + + public static function getSubscribedEvents(): array + { + return [ + // Use a priority of 33 to run before Symfony's router listener. + // @see `\Symfony\Component\HttpKernel\EventListener\RouterListener::getSubscribedEvents` + KernelEvents::REQUEST => ['onRequest', 33], + ]; + } + + public function onRequest(RequestEvent $event): void + { + if ($this->routeProvider instanceof CacheableRouteProviderInterface) { + // Mimics `\Drupal\Core\Cache\Context\UserCacheContext::getContext`. + $this->routeProvider->addExtraCacheKeyPart('user', $this->currentUser->id()); + + $request = $event->getRequest(); + // Mimics `\Drupal\Core\Cache\Context\SiteCacheContext::getContext`. + $this->routeProvider->addExtraCacheKeyPart('url.site', $request->getSchemeAndHttpHost() . $request->getBaseUrl()); + } + } +} diff --git a/src/EventSubscriber/Shield.php b/src/EventSubscriber/Shield.php index f12668aca6b6fb5bc5024b1210efeac0d2bb0416..44d176f3bdc78b574ec74480de5121d4a378e606 100644 --- a/src/EventSubscriber/Shield.php +++ b/src/EventSubscriber/Shield.php @@ -49,7 +49,7 @@ class Shield implements EventSubscriberInterface { /** * {@inheritdoc} */ - static function getSubscribedEvents() { + static function getSubscribedEvents(): array { $events[KernelEvents::REQUEST][] = array('ShieldLoad', 300); return $events; } diff --git a/src/EventSubscriber/SiteRedirectSubscriber.php b/src/EventSubscriber/SiteRedirectSubscriber.php index 04abd156aa96898f1d93ba8c586c586f12b2e8ea..baea551530a4913571c379d1034d9b2c019f1846 100644 --- a/src/EventSubscriber/SiteRedirectSubscriber.php +++ b/src/EventSubscriber/SiteRedirectSubscriber.php @@ -81,7 +81,7 @@ class SiteRedirectSubscriber implements EventSubscriberInterface { /** * {@inheritdoc} */ - public static function getSubscribedEvents() { + public static function getSubscribedEvents(): array { $events[KernelEvents::REQUEST] = array('onKernelRequestSite', 50); return $events; } diff --git a/src/MicroSiteServiceProvider.php b/src/MicroSiteServiceProvider.php index 0715481b90dc393a05fc505b11a3b1211c3e2994..d0258bda6fb9e0647ea44ca2564d05b3606a1fdd 100644 --- a/src/MicroSiteServiceProvider.php +++ b/src/MicroSiteServiceProvider.php @@ -23,21 +23,6 @@ class MicroSiteServiceProvider extends ServiceProviderBase implements ServiceMod public function alter(ContainerBuilder $container) { $definition = $container->getDefinition('router.route_provider'); $definition->setClass(SiteRouteProvider::class); - - if ($container->hasParameter('renderer.config')) { - $renderer_config = $container->getParameter('renderer.config'); - - if (!in_array('url.site', $renderer_config['required_cache_contexts'])) { - $renderer_config['required_cache_contexts'][] = 'url.site'; - } - // Permissions related to site context are based on user referenced by site. - // We need so to add the user as a cache context. - if (!in_array('user', $renderer_config['required_cache_contexts'])) { - $renderer_config['required_cache_contexts'][] = 'user'; - } - - $container->setParameter('renderer.config', $renderer_config); - } } } diff --git a/src/Plugin/Validation/Constraint/RegisteredFieldConstraintValidator.php b/src/Plugin/Validation/Constraint/RegisteredFieldConstraintValidator.php index 6ea3b19bb76244d54569861020f802b0de9854ae..5cabb9923dbe44eb3748a9b335823bd4ef30652e 100644 --- a/src/Plugin/Validation/Constraint/RegisteredFieldConstraintValidator.php +++ b/src/Plugin/Validation/Constraint/RegisteredFieldConstraintValidator.php @@ -32,7 +32,7 @@ class RegisteredFieldConstraintValidator extends ConstraintValidator { } // We cannot know which Guzzle Exception class will be returned; be generic. catch (RequestException $e) { - watchdog_exception('micro_site', $e); + \Drupal\Component\Utility\DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0', fn() => \Drupal\Core\Utility\Error::logException(\Drupal::logger('micro_site'), $e), fn() => watchdog_exception('micro_site', $e)); // File a general server failure. $this->context->addViolation($constraint->message, [ '%value' => $entity->getSiteUrl(), diff --git a/src/StackMiddleware/MicroSiteMiddleware.php b/src/StackMiddleware/MicroSiteMiddleware.php index 58c51e995ebca99f7987180ab261a548f0187aef..218a09015582ff9da569af5919957829e20bf381 100644 --- a/src/StackMiddleware/MicroSiteMiddleware.php +++ b/src/StackMiddleware/MicroSiteMiddleware.php @@ -45,7 +45,7 @@ class MicroSiteMiddleware implements HttpKernelInterface { /** * {@inheritdoc} */ - public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) { + public function handle(Request $request, $type = self::MAIN_REQUEST, $catch = TRUE) { $active_site = $this->negotiator->getActiveSite(); if (empty($active_site)) { return $this->httpKernel->handle($request, $type, $catch); @@ -60,7 +60,7 @@ class MicroSiteMiddleware implements HttpKernelInterface { $pass = $active_site->getSiteShieldPassword(); } - if ($type != self::MASTER_REQUEST || !$shield_enabled || !$user || PHP_SAPI === 'cli') { + if ($type != self::MAIN_REQUEST || !$shield_enabled || !$user || PHP_SAPI === 'cli') { // Bypass: // 1. Subrequests // 2. Empty username