Mercurial > hg > isophonics-drupal-site
diff core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | |
children | 129ea1e6d783 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php Mon Apr 23 09:46:53 2018 +0100 @@ -0,0 +1,85 @@ +<?php + +namespace Drupal\layout_builder\EventSubscriber; + +use Drupal\Core\Access\AccessResult; +use Drupal\Core\Block\BlockPluginInterface; +use Drupal\Core\Session\AccountInterface; +use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent; +use Drupal\layout_builder\LayoutBuilderEvents; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +/** + * Builds render arrays and handles access for all block components. + * + * @internal + * Layout Builder is currently experimental and should only be leveraged by + * experimental modules and development releases of contributed modules. + * See https://www.drupal.org/core/experimental for more information. + */ +class BlockComponentRenderArray implements EventSubscriberInterface { + + /** + * The current user. + * + * @var \Drupal\Core\Session\AccountInterface + */ + protected $currentUser; + + /** + * Creates a BlockComponentRenderArray object. + * + * @param \Drupal\Core\Session\AccountInterface $current_user + * The current user. + */ + public function __construct(AccountInterface $current_user) { + $this->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; + } + + // 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(), + ]; + $event->setBuild($build); + } + } + +}