comparison core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
6 use Drupal\Core\Access\AccessResult; 6 use Drupal\Core\Access\AccessResult;
7 use Drupal\Core\Block\BlockPluginInterface; 7 use Drupal\Core\Block\BlockPluginInterface;
8 use Drupal\Core\Render\Element; 8 use Drupal\Core\Render\Element;
9 use Drupal\Core\Render\PreviewFallbackInterface; 9 use Drupal\Core\Render\PreviewFallbackInterface;
10 use Drupal\Core\Session\AccountInterface; 10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\Core\StringTranslation\StringTranslationTrait;
11 use Drupal\layout_builder\Access\LayoutPreviewAccessAllowed; 12 use Drupal\layout_builder\Access\LayoutPreviewAccessAllowed;
12 use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent; 13 use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
13 use Drupal\layout_builder\LayoutBuilderEvents; 14 use Drupal\layout_builder\LayoutBuilderEvents;
15 use Drupal\views\Plugin\Block\ViewsBlock;
14 use Symfony\Component\EventDispatcher\EventSubscriberInterface; 16 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15 17
16 /** 18 /**
17 * Builds render arrays and handles access for all block components. 19 * Builds render arrays and handles access for all block components.
18 * 20 *
19 * @internal 21 * @internal
20 * Layout Builder is currently experimental and should only be leveraged by 22 * Tagged services are internal.
21 * experimental modules and development releases of contributed modules.
22 * See https://www.drupal.org/core/experimental for more information.
23 */ 23 */
24 class BlockComponentRenderArray implements EventSubscriberInterface { 24 class BlockComponentRenderArray implements EventSubscriberInterface {
25
26 use StringTranslationTrait;
25 27
26 /** 28 /**
27 * The current user. 29 * The current user.
28 * 30 *
29 * @var \Drupal\Core\Session\AccountInterface 31 * @var \Drupal\Core\Session\AccountInterface
88 90
89 $event->addCacheableDependency($access); 91 $event->addCacheableDependency($access);
90 if ($access->isAllowed()) { 92 if ($access->isAllowed()) {
91 $event->addCacheableDependency($block); 93 $event->addCacheableDependency($block);
92 94
95 // @todo Revisit after https://www.drupal.org/node/3027653, as this will
96 // provide a better way to remove contextual links from Views blocks.
97 // Currently, doing this requires setting
98 // \Drupal\views\ViewExecutable::$showAdminLinks() to false before the
99 // Views block is built.
100 if ($block instanceof ViewsBlock && $event->inPreview()) {
101 $block->getViewExecutable()->setShowAdminLinks(FALSE);
102 }
103
93 $content = $block->build(); 104 $content = $block->build();
94 $is_content_empty = Element::isEmpty($content); 105 $is_content_empty = Element::isEmpty($content);
95 $is_placeholder_ready = $event->inPreview() && $block instanceof PreviewFallbackInterface; 106 $is_placeholder_ready = $event->inPreview() && $block instanceof PreviewFallbackInterface;
96 // If the content is empty and no placeholder is available, return. 107 // If the content is empty and no placeholder is available, return.
97 if ($is_content_empty && !$is_placeholder_ready) { 108 if ($is_content_empty && !$is_placeholder_ready) {
106 '#base_plugin_id' => $block->getBaseId(), 117 '#base_plugin_id' => $block->getBaseId(),
107 '#derivative_plugin_id' => $block->getDerivativeId(), 118 '#derivative_plugin_id' => $block->getDerivativeId(),
108 '#weight' => $event->getComponent()->getWeight(), 119 '#weight' => $event->getComponent()->getWeight(),
109 'content' => $content, 120 'content' => $content,
110 ]; 121 ];
122
123 if ($block instanceof PreviewFallbackInterface) {
124 $preview_fallback_string = $block->getPreviewFallbackString();
125 }
126 else {
127 $preview_fallback_string = $this->t('"@block" block', ['@block' => $block->label()]);
128 }
129 // @todo Use new label methods so
130 // data-layout-content-preview-placeholder-label doesn't have to use
131 // preview fallback in https://www.drupal.org/node/2025649.
132 $build['#attributes']['data-layout-content-preview-placeholder-label'] = $preview_fallback_string;
133
111 if ($is_content_empty && $is_placeholder_ready) { 134 if ($is_content_empty && $is_placeholder_ready) {
112 $build['content']['#markup'] = $block->getPreviewFallbackString(); 135 $build['content']['#markup'] = $this->t('Placeholder for the @preview_fallback', ['@preview_fallback' => $block->getPreviewFallbackString()]);
113 } 136 }
114 $event->setBuild($build); 137 $event->setBuild($build);
115 } 138 }
116 } 139 }
117 140