class WorkspaceRequestSubscriber

Same name in other branches
  1. 8.9.x core/modules/workspaces/src/EventSubscriber/WorkspaceRequestSubscriber.php \Drupal\workspaces\EventSubscriber\WorkspaceRequestSubscriber
  2. 10 core/modules/workspaces/src/EventSubscriber/WorkspaceRequestSubscriber.php \Drupal\workspaces\EventSubscriber\WorkspaceRequestSubscriber
  3. 11.x core/modules/workspaces/src/EventSubscriber/WorkspaceRequestSubscriber.php \Drupal\workspaces\EventSubscriber\WorkspaceRequestSubscriber

Provides a event subscriber for setting workspace-specific cache keys.

Hierarchy

  • class \Drupal\workspaces\EventSubscriber\WorkspaceRequestSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of WorkspaceRequestSubscriber

1 file declares its use of WorkspaceRequestSubscriber
WorkspaceRequestSubscriberTest.php in core/modules/workspaces/tests/src/Unit/WorkspaceRequestSubscriberTest.php
1 string reference to 'WorkspaceRequestSubscriber'
workspaces.services.yml in core/modules/workspaces/workspaces.services.yml
core/modules/workspaces/workspaces.services.yml
1 service uses WorkspaceRequestSubscriber
workspaces.workspace_subscriber in core/modules/workspaces/workspaces.services.yml
Drupal\workspaces\EventSubscriber\WorkspaceRequestSubscriber

File

core/modules/workspaces/src/EventSubscriber/WorkspaceRequestSubscriber.php, line 18

Namespace

Drupal\workspaces\EventSubscriber
View source
class WorkspaceRequestSubscriber implements EventSubscriberInterface {
    
    /**
     * The alias manager that caches alias lookups based on the request.
     *
     * @var \Drupal\path_alias\AliasManagerInterface
     */
    protected $aliasManager;
    
    /**
     * The current path.
     *
     * @var \Drupal\Core\Path\CurrentPathStack
     */
    protected $currentPath;
    
    /**
     * The route provider to load routes by name.
     *
     * @var \Drupal\Core\Routing\RouteProviderInterface
     */
    protected $routeProvider;
    
    /**
     * The workspace manager.
     *
     * @var \Drupal\workspaces\WorkspaceManagerInterface
     */
    protected $workspaceManager;
    
    /**
     * Constructs a new WorkspaceRequestSubscriber instance.
     *
     * @param \Drupal\path_alias\AliasManagerInterface $alias_manager
     *   The alias manager.
     * @param \Drupal\Core\Path\CurrentPathStack $current_path
     *   The current path.
     * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
     *   The route provider.
     * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
     *   The workspace manager.
     */
    public function __construct(AliasManagerInterface $alias_manager, CurrentPathStack $current_path, RouteProviderInterface $route_provider, WorkspaceManagerInterface $workspace_manager) {
        $this->aliasManager = $alias_manager;
        $this->currentPath = $current_path;
        $this->routeProvider = $route_provider;
        $this->workspaceManager = $workspace_manager;
    }
    
    /**
     * Sets the cache key on the alias manager cache decorator.
     *
     * KernelEvents::CONTROLLER is used in order to be executed after routing.
     *
     * @param \Symfony\Component\HttpKernel\Event\ControllerEvent $event
     *   The Event to process.
     */
    public function onKernelController(ControllerEvent $event) {
        // Set the cache key on the alias manager cache decorator.
        if ($event->isMainRequest() && $this->workspaceManager
            ->hasActiveWorkspace()) {
            $cache_key = $this->workspaceManager
                ->getActiveWorkspace()
                ->id() . ':' . rtrim($this->currentPath
                ->getPath($event->getRequest()), '/');
            $this->aliasManager
                ->setCacheKey($cache_key);
        }
    }
    
    /**
     * Adds the active workspace as a cache key part to the route provider.
     *
     * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
     *   An event object.
     */
    public function onKernelRequest(RequestEvent $event) {
        if ($this->workspaceManager
            ->hasActiveWorkspace() && $this->routeProvider instanceof CacheableRouteProviderInterface) {
            $this->routeProvider
                ->addExtraCacheKeyPart('workspace', $this->workspaceManager
                ->getActiveWorkspace()
                ->id());
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        // Use a priority of 190 in order to run after the generic core subscriber.
        // @see \Drupal\Core\EventSubscriber\PathSubscriber::getSubscribedEvents()
        $events[KernelEvents::CONTROLLER][] = [
            'onKernelController',
            190,
        ];
        // Use a priority of 33 in order to run before Symfony's router listener.
        // @see \Symfony\Component\HttpKernel\EventListener\RouterListener::getSubscribedEvents()
        $events[KernelEvents::REQUEST][] = [
            'onKernelRequest',
            33,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
WorkspaceRequestSubscriber::$aliasManager protected property The alias manager that caches alias lookups based on the request.
WorkspaceRequestSubscriber::$currentPath protected property The current path.
WorkspaceRequestSubscriber::$routeProvider protected property The route provider to load routes by name.
WorkspaceRequestSubscriber::$workspaceManager protected property The workspace manager.
WorkspaceRequestSubscriber::getSubscribedEvents public static function
WorkspaceRequestSubscriber::onKernelController public function Sets the cache key on the alias manager cache decorator.
WorkspaceRequestSubscriber::onKernelRequest public function Adds the active workspace as a cache key part to the route provider.
WorkspaceRequestSubscriber::__construct public function Constructs a new WorkspaceRequestSubscriber instance.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.