3 namespace Drupal\layout_builder\EventSubscriber;
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;
15 * Builds render arrays and handles access for all block components.
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.
22 class BlockComponentRenderArray implements EventSubscriberInterface {
27 * @var \Drupal\Core\Session\AccountInterface
29 protected $currentUser;
32 * Creates a BlockComponentRenderArray object.
34 * @param \Drupal\Core\Session\AccountInterface $current_user
37 public function __construct(AccountInterface $current_user) {
38 $this->currentUser = $current_user;
44 public static function getSubscribedEvents() {
45 $events[LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY] = ['onBuildRender', 100];
50 * Builds render arrays for block plugins and sets it on the event.
52 * @param \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event
53 * The section component render event.
55 public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
56 $block = $event->getPlugin();
57 if (!$block instanceof BlockPluginInterface) {
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());
73 $block->setAccessDependency($entity);
79 // Only check access if the component is not being previewed.
80 if ($event->inPreview()) {
81 $access = AccessResult::allowed()->setCacheMaxAge(0);
84 $access = $block->access($this->currentUser, TRUE);
87 $event->addCacheableDependency($access);
88 if ($access->isAllowed()) {
89 $event->addCacheableDependency($block);
92 // @todo Move this to BlockBase in https://www.drupal.org/node/2931040.
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(),
101 $event->setBuild($build);