Chris@14: currentUser = $current_user; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public static function getSubscribedEvents() { Chris@14: $events[LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY] = ['onBuildRender', 100]; Chris@14: return $events; Chris@14: } Chris@14: Chris@14: /** Chris@14: * Builds render arrays for block plugins and sets it on the event. Chris@14: * Chris@14: * @param \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event Chris@14: * The section component render event. Chris@14: */ Chris@14: public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) { Chris@14: $block = $event->getPlugin(); Chris@14: if (!$block instanceof BlockPluginInterface) { Chris@14: return; Chris@14: } Chris@14: Chris@17: // Set block access dependency even if we are not checking access on Chris@17: // this level. The block itself may render another Chris@17: // RefinableDependentAccessInterface object and need to pass on this value. Chris@17: if ($block instanceof RefinableDependentAccessInterface) { Chris@17: $contexts = $event->getContexts(); Chris@17: if (isset($contexts['layout_builder.entity'])) { Chris@17: if ($entity = $contexts['layout_builder.entity']->getContextValue()) { Chris@17: if ($event->inPreview()) { Chris@17: // If previewing in Layout Builder allow access. Chris@17: $block->setAccessDependency(new LayoutPreviewAccessAllowed()); Chris@17: } Chris@17: else { Chris@17: $block->setAccessDependency($entity); Chris@17: } Chris@17: } Chris@17: } Chris@17: } Chris@17: Chris@14: // Only check access if the component is not being previewed. Chris@14: if ($event->inPreview()) { Chris@14: $access = AccessResult::allowed()->setCacheMaxAge(0); Chris@14: } Chris@14: else { Chris@14: $access = $block->access($this->currentUser, TRUE); Chris@14: } Chris@14: Chris@14: $event->addCacheableDependency($access); Chris@14: if ($access->isAllowed()) { Chris@14: $event->addCacheableDependency($block); Chris@14: Chris@18: // @todo Revisit after https://www.drupal.org/node/3027653, as this will Chris@18: // provide a better way to remove contextual links from Views blocks. Chris@18: // Currently, doing this requires setting Chris@18: // \Drupal\views\ViewExecutable::$showAdminLinks() to false before the Chris@18: // Views block is built. Chris@18: if ($block instanceof ViewsBlock && $event->inPreview()) { Chris@18: $block->getViewExecutable()->setShowAdminLinks(FALSE); Chris@18: } Chris@18: Chris@17: $content = $block->build(); Chris@17: $is_content_empty = Element::isEmpty($content); Chris@17: $is_placeholder_ready = $event->inPreview() && $block instanceof PreviewFallbackInterface; Chris@17: // If the content is empty and no placeholder is available, return. Chris@17: if ($is_content_empty && !$is_placeholder_ready) { Chris@17: return; Chris@17: } Chris@17: Chris@14: $build = [ Chris@14: // @todo Move this to BlockBase in https://www.drupal.org/node/2931040. Chris@14: '#theme' => 'block', Chris@14: '#configuration' => $block->getConfiguration(), Chris@14: '#plugin_id' => $block->getPluginId(), Chris@14: '#base_plugin_id' => $block->getBaseId(), Chris@14: '#derivative_plugin_id' => $block->getDerivativeId(), Chris@14: '#weight' => $event->getComponent()->getWeight(), Chris@17: 'content' => $content, Chris@14: ]; Chris@18: Chris@18: if ($block instanceof PreviewFallbackInterface) { Chris@18: $preview_fallback_string = $block->getPreviewFallbackString(); Chris@18: } Chris@18: else { Chris@18: $preview_fallback_string = $this->t('"@block" block', ['@block' => $block->label()]); Chris@18: } Chris@18: // @todo Use new label methods so Chris@18: // data-layout-content-preview-placeholder-label doesn't have to use Chris@18: // preview fallback in https://www.drupal.org/node/2025649. Chris@18: $build['#attributes']['data-layout-content-preview-placeholder-label'] = $preview_fallback_string; Chris@18: Chris@17: if ($is_content_empty && $is_placeholder_ready) { Chris@18: $build['content']['#markup'] = $this->t('Placeholder for the @preview_fallback', ['@preview_fallback' => $block->getPreviewFallbackString()]); Chris@17: } Chris@14: $event->setBuild($build); Chris@14: } Chris@14: } Chris@14: Chris@14: }