Issue #3088077 by Sam152, bkosborne, rensingh99, tim.plunkett: Layout builder does not correctly bubble up cache metadata for empty blocks

merge-requests/2419/head
Alex Pott 2020-02-13 09:31:58 +00:00
parent f8127f8f27
commit 8e37bfb03f
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
2 changed files with 22 additions and 4 deletions

View File

@ -5,6 +5,7 @@ namespace Drupal\layout_builder\EventSubscriber;
use Drupal\block_content\Access\RefinableDependentAccessInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\PreviewFallbackInterface;
use Drupal\Core\Session\AccountInterface;
@ -102,6 +103,12 @@ class BlockComponentRenderArray implements EventSubscriberInterface {
}
$content = $block->build();
// We don't output the block render data if there are no render elements
// found, but we want to capture the cache metadata from the block
// regardless.
$event->addCacheableDependency(CacheableMetadata::createFromRenderArray($content));
$is_content_empty = Element::isEmpty($content);
$is_placeholder_ready = $event->inPreview() && $block instanceof PreviewFallbackInterface;
// If the content is empty and no placeholder is available, return.

View File

@ -99,7 +99,10 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
$placeholder_label = 'Placeholder Label';
$block->getPreviewFallbackString()->willReturn($placeholder_label);
$block_content = ['#markup' => 'The block content.'];
$block_content = [
'#markup' => 'The block content.',
'#cache' => ['tags' => ['build-tag']],
];
$block->build()->willReturn($block_content);
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
@ -122,7 +125,10 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
$expected_cache = $expected_build + [
'#cache' => [
'contexts' => [],
'tags' => ['test'],
'tags' => [
'build-tag',
'test',
],
'max-age' => -1,
],
];
@ -410,7 +416,9 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
$block->getBaseId()->willReturn('block_plugin_id');
$block->getDerivativeId()->willReturn(NULL);
$block->build()->willReturn([]);
$block->build()->willReturn([
'#cache' => ['tags' => ['build-tag']],
]);
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
$component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
@ -423,7 +431,10 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
$expected_cache = $expected_build + [
'#cache' => [
'contexts' => [],
'tags' => ['test'],
'tags' => [
'build-tag',
'test',
],
'max-age' => -1,
],
];