diff -u b/src/Plugin/DisplayBuilder/StandardDisplayBuilder.php b/src/Plugin/DisplayBuilder/StandardDisplayBuilder.php --- b/src/Plugin/DisplayBuilder/StandardDisplayBuilder.php +++ b/src/Plugin/DisplayBuilder/StandardDisplayBuilder.php @@ -120,17 +120,26 @@ $content = $block->build(); if (!is_array($content)) { - throw new \LogicException(new FormattableMarkup('Block %block build method has to return an array.', ['%block' => $block_id])); + $class = get_class($block); + throw new \LogicException(sprintf('BlockPluginInterface::build method has to return an array. Class returning non-array value is: %s', $class)); } - // If the block is not empty, build the block and bubble its - // attributes up if possible. This allows modules like Quickedit to - // function. // If the block is empty, instead of trying to render the block // correctly return just #cache, so that the render system knows the // reasons (cache contexts & tags) why this block is empty. // @see \Drupal\block\BlockViewBuilder::preRender() - if (!Element::isEmpty($content)) { + if (Element::isEmpty($content)) { + $block_render_array = []; + $cacheable_metadata = CacheableMetadata::createFromObject($block_render_array); + $cacheable_metadata->applyTo($block_render_array); + if (isset($content['#cache'])) { + $block_render_array['#cache'] = $content['#cache']; + } + } + else { + // If the block is not empty, build the block and bubble its + // attributes up if possible. This allows modules like Quickedit to + // function. foreach (['#attributes', '#contextual_links'] as $property) { if (isset($content[$property])) { $block_render_array[$property] += $content[$property]; @@ -139,14 +148,6 @@ } $block_render_array['content'] = $content; } - else { - $block_render_array = []; - $cacheable_metadata = CacheableMetadata::createFromObject($block_render_array); - $cacheable_metadata->applyTo($block_render_array); - if (isset($content['#cache'])) { - $block_render_array['#cache'] = $content['#cache']; - } - } $build[$region][$block_id] = $block_render_array; }