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