Mercurial > hg > isophonics-drupal-site
comparison core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php @ 17:129ea1e6d783
Update, including to Drupal core 8.6.10
author | Chris Cannam |
---|---|
date | Thu, 28 Feb 2019 13:21:36 +0000 |
parents | 1fec387a4317 |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
16:c2387f117808 | 17:129ea1e6d783 |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\layout_builder\EventSubscriber; | 3 namespace Drupal\layout_builder\EventSubscriber; |
4 | 4 |
5 use Drupal\block_content\Access\RefinableDependentAccessInterface; | |
5 use Drupal\Core\Access\AccessResult; | 6 use Drupal\Core\Access\AccessResult; |
6 use Drupal\Core\Block\BlockPluginInterface; | 7 use Drupal\Core\Block\BlockPluginInterface; |
8 use Drupal\Core\Render\Element; | |
9 use Drupal\Core\Render\PreviewFallbackInterface; | |
7 use Drupal\Core\Session\AccountInterface; | 10 use Drupal\Core\Session\AccountInterface; |
11 use Drupal\layout_builder\Access\LayoutPreviewAccessAllowed; | |
8 use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent; | 12 use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent; |
9 use Drupal\layout_builder\LayoutBuilderEvents; | 13 use Drupal\layout_builder\LayoutBuilderEvents; |
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface; | 14 use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
11 | 15 |
12 /** | 16 /** |
54 $block = $event->getPlugin(); | 58 $block = $event->getPlugin(); |
55 if (!$block instanceof BlockPluginInterface) { | 59 if (!$block instanceof BlockPluginInterface) { |
56 return; | 60 return; |
57 } | 61 } |
58 | 62 |
63 // Set block access dependency even if we are not checking access on | |
64 // this level. The block itself may render another | |
65 // RefinableDependentAccessInterface object and need to pass on this value. | |
66 if ($block instanceof RefinableDependentAccessInterface) { | |
67 $contexts = $event->getContexts(); | |
68 if (isset($contexts['layout_builder.entity'])) { | |
69 if ($entity = $contexts['layout_builder.entity']->getContextValue()) { | |
70 if ($event->inPreview()) { | |
71 // If previewing in Layout Builder allow access. | |
72 $block->setAccessDependency(new LayoutPreviewAccessAllowed()); | |
73 } | |
74 else { | |
75 $block->setAccessDependency($entity); | |
76 } | |
77 } | |
78 } | |
79 } | |
80 | |
59 // Only check access if the component is not being previewed. | 81 // Only check access if the component is not being previewed. |
60 if ($event->inPreview()) { | 82 if ($event->inPreview()) { |
61 $access = AccessResult::allowed()->setCacheMaxAge(0); | 83 $access = AccessResult::allowed()->setCacheMaxAge(0); |
62 } | 84 } |
63 else { | 85 else { |
66 | 88 |
67 $event->addCacheableDependency($access); | 89 $event->addCacheableDependency($access); |
68 if ($access->isAllowed()) { | 90 if ($access->isAllowed()) { |
69 $event->addCacheableDependency($block); | 91 $event->addCacheableDependency($block); |
70 | 92 |
93 $content = $block->build(); | |
94 $is_content_empty = Element::isEmpty($content); | |
95 $is_placeholder_ready = $event->inPreview() && $block instanceof PreviewFallbackInterface; | |
96 // If the content is empty and no placeholder is available, return. | |
97 if ($is_content_empty && !$is_placeholder_ready) { | |
98 return; | |
99 } | |
100 | |
71 $build = [ | 101 $build = [ |
72 // @todo Move this to BlockBase in https://www.drupal.org/node/2931040. | 102 // @todo Move this to BlockBase in https://www.drupal.org/node/2931040. |
73 '#theme' => 'block', | 103 '#theme' => 'block', |
74 '#configuration' => $block->getConfiguration(), | 104 '#configuration' => $block->getConfiguration(), |
75 '#plugin_id' => $block->getPluginId(), | 105 '#plugin_id' => $block->getPluginId(), |
76 '#base_plugin_id' => $block->getBaseId(), | 106 '#base_plugin_id' => $block->getBaseId(), |
77 '#derivative_plugin_id' => $block->getDerivativeId(), | 107 '#derivative_plugin_id' => $block->getDerivativeId(), |
78 '#weight' => $event->getComponent()->getWeight(), | 108 '#weight' => $event->getComponent()->getWeight(), |
79 'content' => $block->build(), | 109 'content' => $content, |
80 ]; | 110 ]; |
111 if ($is_content_empty && $is_placeholder_ready) { | |
112 $build['content']['#markup'] = $block->getPreviewFallbackString(); | |
113 } | |
81 $event->setBuild($build); | 114 $event->setBuild($build); |
82 } | 115 } |
83 } | 116 } |
84 | 117 |
85 } | 118 } |