Browse Source

Issue #3173946 by Charlie ChX Negyesi, Chi: drupal_region() overcaches empty

3.1.x
Chi 4 years ago
parent
commit
d1f528277c
  1. 7
      src/View/RegionViewBuilder.php
  2. 10
      tests/src/Kernel/RegionViewBuilderTest.php

7
src/View/RegionViewBuilder.php

@ -81,7 +81,10 @@ class RegionViewBuilder {
$build = []; $build = [];
$cache_metadata = new CacheableMetadata(); $entity_type = $this->entityTypeManager->getDefinition('block');
$cache_metadata = (new CacheableMetadata())
->addCacheTags($entity_type->getListCacheTags())
->addCacheContexts($entity_type->getListCacheContexts());
/** @var \Drupal\block\BlockInterface[] $blocks */ /** @var \Drupal\block\BlockInterface[] $blocks */
foreach ($blocks as $id => $block) { foreach ($blocks as $id => $block) {
@ -102,8 +105,8 @@ class RegionViewBuilder {
if ($build) { if ($build) {
$build['#region'] = $region; $build['#region'] = $region;
$build['#theme_wrappers'] = ['region']; $build['#theme_wrappers'] = ['region'];
$cache_metadata->applyTo($build);
} }
$cache_metadata->applyTo($build);
return $build; return $build;
} }

10
tests/src/Kernel/RegionViewBuilderTest.php

@ -63,7 +63,14 @@ final class RegionViewBuilderTest extends KernelTestBase {
$build = $view_builder->build('sidebar_first'); $build = $view_builder->build('sidebar_first');
// The build should be empty because 'stable' is not a default theme. // The build should be empty because 'stable' is not a default theme.
self::assertSame([], $build); $expected_build = [
'#cache' => [
'contexts' => [],
'tags' => ['config:block_list'],
'max-age' => Cache::PERMANENT,
],
];
self::assertSame($expected_build, $build);
// Specify the theme name explicitly. // Specify the theme name explicitly.
$build = $view_builder->build('sidebar_first', 'stable'); $build = $view_builder->build('sidebar_first', 'stable');
@ -103,6 +110,7 @@ final class RegionViewBuilderTest extends KernelTestBase {
'contexts' => ['user'], 'contexts' => ['user'],
'tags' => [ 'tags' => [
'config:block.block.public_block', 'config:block.block.public_block',
'config:block_list',
'tag_for_private_block', 'tag_for_private_block',
'tag_for_public_block', 'tag_for_public_block',
], ],

Loading…
Cancel
Save