Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / layout_builder / src / EventSubscriber / BlockComponentRenderArray.php
1 <?php
2
3 namespace Drupal\layout_builder\EventSubscriber;
4
5 use Drupal\block_content\Access\RefinableDependentAccessInterface;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\Core\Block\BlockPluginInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\layout_builder\Access\LayoutPreviewAccessAllowed;
10 use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
11 use Drupal\layout_builder\LayoutBuilderEvents;
12 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14 /**
15  * Builds render arrays and handles access for all block components.
16  *
17  * @internal
18  *   Layout Builder is currently experimental and should only be leveraged by
19  *   experimental modules and development releases of contributed modules.
20  *   See https://www.drupal.org/core/experimental for more information.
21  */
22 class BlockComponentRenderArray implements EventSubscriberInterface {
23
24   /**
25    * The current user.
26    *
27    * @var \Drupal\Core\Session\AccountInterface
28    */
29   protected $currentUser;
30
31   /**
32    * Creates a BlockComponentRenderArray object.
33    *
34    * @param \Drupal\Core\Session\AccountInterface $current_user
35    *   The current user.
36    */
37   public function __construct(AccountInterface $current_user) {
38     $this->currentUser = $current_user;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public static function getSubscribedEvents() {
45     $events[LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY] = ['onBuildRender', 100];
46     return $events;
47   }
48
49   /**
50    * Builds render arrays for block plugins and sets it on the event.
51    *
52    * @param \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event
53    *   The section component render event.
54    */
55   public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
56     $block = $event->getPlugin();
57     if (!$block instanceof BlockPluginInterface) {
58       return;
59     }
60
61     // Set block access dependency even if we are not checking access on
62     // this level. The block itself may render another
63     // RefinableDependentAccessInterface object and need to pass on this value.
64     if ($block instanceof RefinableDependentAccessInterface) {
65       $contexts = $event->getContexts();
66       if (isset($contexts['layout_builder.entity'])) {
67         if ($entity = $contexts['layout_builder.entity']->getContextValue()) {
68           if ($event->inPreview()) {
69             // If previewing in Layout Builder allow access.
70             $block->setAccessDependency(new LayoutPreviewAccessAllowed());
71           }
72           else {
73             $block->setAccessDependency($entity);
74           }
75         }
76       }
77     }
78
79     // Only check access if the component is not being previewed.
80     if ($event->inPreview()) {
81       $access = AccessResult::allowed()->setCacheMaxAge(0);
82     }
83     else {
84       $access = $block->access($this->currentUser, TRUE);
85     }
86
87     $event->addCacheableDependency($access);
88     if ($access->isAllowed()) {
89       $event->addCacheableDependency($block);
90
91       $build = [
92         // @todo Move this to BlockBase in https://www.drupal.org/node/2931040.
93         '#theme' => 'block',
94         '#configuration' => $block->getConfiguration(),
95         '#plugin_id' => $block->getPluginId(),
96         '#base_plugin_id' => $block->getBaseId(),
97         '#derivative_plugin_id' => $block->getDerivativeId(),
98         '#weight' => $event->getComponent()->getWeight(),
99         'content' => $block->build(),
100       ];
101       $event->setBuild($build);
102     }
103   }
104
105 }