annotate core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php @ 15:e200cb7efeb3

Update Drupal core to 8.5.3 via Composer
author Chris Cannam
date Thu, 26 Apr 2018 11:26:54 +0100
parents 1fec387a4317
children 129ea1e6d783
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 namespace Drupal\layout_builder\EventSubscriber;
Chris@14 4
Chris@14 5 use Drupal\Core\Access\AccessResult;
Chris@14 6 use Drupal\Core\Block\BlockPluginInterface;
Chris@14 7 use Drupal\Core\Session\AccountInterface;
Chris@14 8 use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
Chris@14 9 use Drupal\layout_builder\LayoutBuilderEvents;
Chris@14 10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@14 11
Chris@14 12 /**
Chris@14 13 * Builds render arrays and handles access for all block components.
Chris@14 14 *
Chris@14 15 * @internal
Chris@14 16 * Layout Builder is currently experimental and should only be leveraged by
Chris@14 17 * experimental modules and development releases of contributed modules.
Chris@14 18 * See https://www.drupal.org/core/experimental for more information.
Chris@14 19 */
Chris@14 20 class BlockComponentRenderArray implements EventSubscriberInterface {
Chris@14 21
Chris@14 22 /**
Chris@14 23 * The current user.
Chris@14 24 *
Chris@14 25 * @var \Drupal\Core\Session\AccountInterface
Chris@14 26 */
Chris@14 27 protected $currentUser;
Chris@14 28
Chris@14 29 /**
Chris@14 30 * Creates a BlockComponentRenderArray object.
Chris@14 31 *
Chris@14 32 * @param \Drupal\Core\Session\AccountInterface $current_user
Chris@14 33 * The current user.
Chris@14 34 */
Chris@14 35 public function __construct(AccountInterface $current_user) {
Chris@14 36 $this->currentUser = $current_user;
Chris@14 37 }
Chris@14 38
Chris@14 39 /**
Chris@14 40 * {@inheritdoc}
Chris@14 41 */
Chris@14 42 public static function getSubscribedEvents() {
Chris@14 43 $events[LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY] = ['onBuildRender', 100];
Chris@14 44 return $events;
Chris@14 45 }
Chris@14 46
Chris@14 47 /**
Chris@14 48 * Builds render arrays for block plugins and sets it on the event.
Chris@14 49 *
Chris@14 50 * @param \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event
Chris@14 51 * The section component render event.
Chris@14 52 */
Chris@14 53 public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
Chris@14 54 $block = $event->getPlugin();
Chris@14 55 if (!$block instanceof BlockPluginInterface) {
Chris@14 56 return;
Chris@14 57 }
Chris@14 58
Chris@14 59 // Only check access if the component is not being previewed.
Chris@14 60 if ($event->inPreview()) {
Chris@14 61 $access = AccessResult::allowed()->setCacheMaxAge(0);
Chris@14 62 }
Chris@14 63 else {
Chris@14 64 $access = $block->access($this->currentUser, TRUE);
Chris@14 65 }
Chris@14 66
Chris@14 67 $event->addCacheableDependency($access);
Chris@14 68 if ($access->isAllowed()) {
Chris@14 69 $event->addCacheableDependency($block);
Chris@14 70
Chris@14 71 $build = [
Chris@14 72 // @todo Move this to BlockBase in https://www.drupal.org/node/2931040.
Chris@14 73 '#theme' => 'block',
Chris@14 74 '#configuration' => $block->getConfiguration(),
Chris@14 75 '#plugin_id' => $block->getPluginId(),
Chris@14 76 '#base_plugin_id' => $block->getBaseId(),
Chris@14 77 '#derivative_plugin_id' => $block->getDerivativeId(),
Chris@14 78 '#weight' => $event->getComponent()->getWeight(),
Chris@14 79 'content' => $block->build(),
Chris@14 80 ];
Chris@14 81 $event->setBuild($build);
Chris@14 82 }
Chris@14 83 }
Chris@14 84
Chris@14 85 }