BlockPlaceEventSubscriber.php in Drupal 8
File
core/modules/block_place/src/EventSubscriber/BlockPlaceEventSubscriber.php
View source
<?php
namespace Drupal\block_place\EventSubscriber;
use Drupal\Core\Render\PageDisplayVariantSelectionEvent;
use Drupal\Core\Render\RenderEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\Core\Session\AccountInterface;
class BlockPlaceEventSubscriber implements EventSubscriberInterface {
protected $requestStack;
protected $account;
public function __construct(RequestStack $request_stack, AccountInterface $account) {
$this->requestStack = $request_stack;
$this->account = $account;
}
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',
]);
}
}
public static function getSubscribedEvents() {
$events[RenderEvents::SELECT_PAGE_DISPLAY_VARIANT][] = [
'onBlockPageDisplayVariantSelected',
-1000,
];
return $events;
}
}