Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\block_place\EventSubscriber;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Render\PageDisplayVariantSelectionEvent;
|
Chris@0
|
6 use Drupal\Core\Render\RenderEvents;
|
Chris@0
|
7 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
Chris@0
|
8 use Symfony\Component\HttpFoundation\RequestStack;
|
Chris@0
|
9 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * @see \Drupal\block_place\Plugin\DisplayVariant\PlaceBlockPageVariant
|
Chris@0
|
13 */
|
Chris@0
|
14 class BlockPlaceEventSubscriber implements EventSubscriberInterface {
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * The request stack.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var \Symfony\Component\HttpFoundation\RequestStack
|
Chris@0
|
20 */
|
Chris@0
|
21 protected $requestStack;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * The current user.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var \Drupal\Core\Session\AccountInterface
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $account;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * Constructs a \Drupal\block_place\EventSubscriber\BlockPlaceEventSubscriber object.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
|
Chris@0
|
34 * The request stack used to retrieve the current request.
|
Chris@0
|
35 * @param \Drupal\Core\Session\AccountInterface $account
|
Chris@0
|
36 * The current user.
|
Chris@0
|
37 */
|
Chris@0
|
38 public function __construct(RequestStack $request_stack, AccountInterface $account) {
|
Chris@0
|
39 $this->requestStack = $request_stack;
|
Chris@0
|
40 $this->account = $account;
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Selects the block place override of the block page display variant.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @param \Drupal\Core\Render\PageDisplayVariantSelectionEvent $event
|
Chris@0
|
47 * The event to process.
|
Chris@0
|
48 */
|
Chris@0
|
49 public function onBlockPageDisplayVariantSelected(PageDisplayVariantSelectionEvent $event) {
|
Chris@0
|
50 if ($event->getPluginId() === 'block_page') {
|
Chris@0
|
51 if ($this->requestStack->getCurrentRequest()->query->has('block-place') && $this->account->hasPermission('administer blocks')) {
|
Chris@0
|
52 $event->setPluginId('block_place_page');
|
Chris@0
|
53 }
|
Chris@0
|
54 $event->addCacheContexts(['user.permissions', 'url.query_args']);
|
Chris@0
|
55 }
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * {@inheritdoc}
|
Chris@0
|
60 */
|
Chris@0
|
61 public static function getSubscribedEvents() {
|
Chris@0
|
62 // Set a very low priority, so that it runs last.
|
Chris@0
|
63 $events[RenderEvents::SELECT_PAGE_DISPLAY_VARIANT][] = ['onBlockPageDisplayVariantSelected', -1000];
|
Chris@0
|
64 return $events;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 }
|