Browse Source

Add cache keys to block build

merge-requests/2/head
Chi 5 years ago
parent
commit
14e6be65b2
  1. 7
      src/View/BlockViewBuilder.php
  2. 16
      tests/src/Kernel/BlockViewBuilderTest.php

7
src/View/BlockViewBuilder.php

@ -147,8 +147,15 @@ class BlockViewBuilder {
CacheableMetadata::createFromRenderArray($build)
->merge(CacheableMetadata::createFromObject($access))
->merge(CacheableMetadata::createFromObject($block_plugin))
->applyTo($build);
if (!isset($build['#cache']['keys'])) {
$configuration['_wrapper'] = $wrapper;
$configuration_hash = hash('sha256', serialize($configuration));
$build['#cache']['keys'] = ['twig_tweak_block', $id, $configuration_hash];
}
return $build;
}

16
tests/src/Kernel/BlockViewBuilderTest.php

@ -62,6 +62,11 @@ final class BlockViewBuilderTest extends KernelTestBase {
'contexts' => ['user'],
'tags' => ['tag_from_blockAccess'],
'max-age' => 35,
'keys' => [
'twig_tweak_block',
'twig_tweak_test_foo',
'9ef25a303eb97a01011ea20c50e731590c000f8bc6196241020fc118cce68180',
],
],
];
self::assertSame($expected_build, $build);
@ -72,6 +77,7 @@ final class BlockViewBuilderTest extends KernelTestBase {
$expected_build['content']['#markup'] = 'Bar';
$expected_build['#configuration']['label'] = 'Example';
$expected_build['#configuration']['content'] = 'Bar';
$expected_build['#cache']['keys'][2] = '96b740819b50d416148484351f52de1c894b63cf59af255b502c9399c73b8557';
self::assertSame($expected_build, $build);
self::assertSame('<div><h2>Example</h2>Bar</div>', $this->renderPlain($build));
@ -89,6 +95,11 @@ final class BlockViewBuilderTest extends KernelTestBase {
'contexts' => ['user'],
'tags' => ['tag_from_blockAccess'],
'max-age' => 35,
'keys' => [
'twig_tweak_block',
'twig_tweak_test_foo',
'82b254ef229c95f336386ee50454e0cc56705e97c1991d68fe9afdb1fb401f3a',
],
],
];
self::assertSame($expected_build, $build);
@ -102,6 +113,11 @@ final class BlockViewBuilderTest extends KernelTestBase {
'contexts' => ['user'],
'tags' => ['tag_from_blockAccess'],
'max-age' => 35,
'keys' => [
'twig_tweak_block',
'twig_tweak_test_foo',
'9ef25a303eb97a01011ea20c50e731590c000f8bc6196241020fc118cce68180',
],
],
];
self::assertSame($expected_build, $build);

Loading…
Cancel
Save