class BlockPlaceEventSubscriber

Hierarchy

  • class \Drupal\block_place\EventSubscriber\BlockPlaceEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of BlockPlaceEventSubscriber

See also

\Drupal\block_place\Plugin\DisplayVariant\PlaceBlockPageVariant

1 string reference to 'BlockPlaceEventSubscriber'
block_place.services.yml in core/modules/block_place/block_place.services.yml
core/modules/block_place/block_place.services.yml
1 service uses BlockPlaceEventSubscriber
block_place.page_display_variant_subscriber.block in core/modules/block_place/block_place.services.yml
Drupal\block_place\EventSubscriber\BlockPlaceEventSubscriber

File

core/modules/block_place/src/EventSubscriber/BlockPlaceEventSubscriber.php, line 14

Namespace

Drupal\block_place\EventSubscriber
View source
class BlockPlaceEventSubscriber implements EventSubscriberInterface {
    
    /**
     * The request stack.
     *
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    protected $requestStack;
    
    /**
     * The current user.
     *
     * @var \Drupal\Core\Session\AccountInterface
     */
    protected $account;
    
    /**
     * Constructs a \Drupal\block_place\EventSubscriber\BlockPlaceEventSubscriber object.
     *
     * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
     *   The request stack used to retrieve the current request.
     * @param \Drupal\Core\Session\AccountInterface $account
     *   The current user.
     */
    public function __construct(RequestStack $request_stack, AccountInterface $account) {
        $this->requestStack = $request_stack;
        $this->account = $account;
    }
    
    /**
     * Selects the block place override of the block page display variant.
     *
     * @param \Drupal\Core\Render\PageDisplayVariantSelectionEvent $event
     *   The event to process.
     */
    public function onBlockPageDisplayVariantSelected(PageDisplayVariantSelectionEvent $event) {
        if ($event->getPluginId() === 'block_page') {
            if ($this->requestStack
                ->getCurrentRequest()->query
                ->has('block-place') && $this->account
                ->hasPermission('administer blocks')) {
                $event->setPluginId('block_place_page');
            }
            $event->addCacheContexts([
                'user.permissions',
                'url.query_args',
            ]);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() {
        // Set a very low priority, so that it runs last.
        $events[RenderEvents::SELECT_PAGE_DISPLAY_VARIANT][] = [
            'onBlockPageDisplayVariantSelected',
            -1000,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
BlockPlaceEventSubscriber::$account protected property The current user.
BlockPlaceEventSubscriber::$requestStack protected property The request stack.
BlockPlaceEventSubscriber::getSubscribedEvents public static function
BlockPlaceEventSubscriber::onBlockPageDisplayVariantSelected public function Selects the block place override of the block page display variant.
BlockPlaceEventSubscriber::__construct public function Constructs a \Drupal\block_place\EventSubscriber\BlockPlaceEventSubscriber object.

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