currentUser = $current_user; } /** * {@inheritdoc} */ public static function getSubscribedEvents() { $events[LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY] = ['onBuildRender', 100]; return $events; } /** * Builds render arrays for block plugins and sets it on the event. * * @param \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event * The section component render event. */ public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) { $block = $event->getPlugin(); if (!$block instanceof BlockPluginInterface) { return; } // Set block access dependency even if we are not checking access on // this level. The block itself may render another // RefinableDependentAccessInterface object and need to pass on this value. if ($block instanceof RefinableDependentAccessInterface) { $contexts = $event->getContexts(); if (isset($contexts['layout_builder.entity'])) { if ($entity = $contexts['layout_builder.entity']->getContextValue()) { if ($event->inPreview()) { // If previewing in Layout Builder allow access. $block->setAccessDependency(new LayoutPreviewAccessAllowed()); } else { $block->setAccessDependency($entity); } } } } // Only check access if the component is not being previewed. if ($event->inPreview()) { $access = AccessResult::allowed()->setCacheMaxAge(0); } else { $access = $block->access($this->currentUser, TRUE); } $event->addCacheableDependency($access); if ($access->isAllowed()) { $event->addCacheableDependency($block); $build = [ // @todo Move this to BlockBase in https://www.drupal.org/node/2931040. '#theme' => 'block', '#configuration' => $block->getConfiguration(), '#plugin_id' => $block->getPluginId(), '#base_plugin_id' => $block->getBaseId(), '#derivative_plugin_id' => $block->getDerivativeId(), '#weight' => $event->getComponent()->getWeight(), 'content' => $block->build(), ]; if ($event->inPreview() && Element::isEmpty($build['content']) && $block instanceof PreviewFallbackInterface) { $build['content']['#markup'] = $block->getPreviewFallbackString(); } $event->setBuild($build); } } }