BlockComponentRenderArray.php in Drupal 8
File
core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php
View source
<?php
namespace Drupal\layout_builder\EventSubscriber;
use Drupal\block_content\Access\RefinableDependentAccessInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\PreviewFallbackInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\layout_builder\Access\LayoutPreviewAccessAllowed;
use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
use Drupal\layout_builder\LayoutBuilderEvents;
use Drupal\views\Plugin\Block\ViewsBlock;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BlockComponentRenderArray implements EventSubscriberInterface {
use StringTranslationTrait;
protected $currentUser;
public function __construct(AccountInterface $current_user) {
$this->currentUser = $current_user;
}
public static function getSubscribedEvents() {
$events[LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY] = [
'onBuildRender',
100,
];
return $events;
}
public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
$block = $event
->getPlugin();
if (!$block instanceof BlockPluginInterface) {
return;
}
if ($block instanceof RefinableDependentAccessInterface) {
$contexts = $event
->getContexts();
if (isset($contexts['layout_builder.entity'])) {
if ($entity = $contexts['layout_builder.entity']
->getContextValue()) {
if ($event
->inPreview()) {
$block
->setAccessDependency(new LayoutPreviewAccessAllowed());
}
else {
$block
->setAccessDependency($entity);
}
}
}
}
if ($event
->inPreview()) {
$access = AccessResult::allowed()
->setCacheMaxAge(0);
}
else {
$access = $block
->access($this->currentUser, TRUE);
}
$event
->addCacheableDependency($access);
if ($access
->isAllowed()) {
$event
->addCacheableDependency($block);
if ($block instanceof ViewsBlock && $event
->inPreview()) {
$block
->getViewExecutable()
->setShowAdminLinks(FALSE);
}
$content = $block
->build();
$event
->addCacheableDependency(CacheableMetadata::createFromRenderArray($content));
$is_content_empty = Element::isEmpty($content);
$is_placeholder_ready = $event
->inPreview() && $block instanceof PreviewFallbackInterface;
if ($is_content_empty && !$is_placeholder_ready) {
return;
}
$build = [
'#theme' => 'block',
'#configuration' => $block
->getConfiguration(),
'#plugin_id' => $block
->getPluginId(),
'#base_plugin_id' => $block
->getBaseId(),
'#derivative_plugin_id' => $block
->getDerivativeId(),
'#weight' => $event
->getComponent()
->getWeight(),
'content' => $content,
];
if ($event
->inPreview()) {
if ($block instanceof PreviewFallbackInterface) {
$preview_fallback_string = $block
->getPreviewFallbackString();
}
else {
$preview_fallback_string = $this
->t('"@block" block', [
'@block' => $block
->label(),
]);
}
$build['#attributes']['data-layout-content-preview-placeholder-label'] = $preview_fallback_string;
if ($is_content_empty && $is_placeholder_ready) {
$build['content']['#markup'] = $this
->t('Placeholder for the @preview_fallback', [
'@preview_fallback' => $block
->getPreviewFallbackString(),
]);
}
}
$event
->setBuild($build);
}
}
}