From 3b17ed3500ea19c63a20b53a6d3d4b845b9695e6 Mon Sep 17 00:00:00 2001 From: Chi Date: Mon, 13 Sep 2021 11:14:07 +0500 Subject: [PATCH] Code clean-up --- src/TwigTweakExtension.php | 2 +- src/View/BlockViewBuilder.php | 4 ++-- src/View/EntityFormViewBuilder.php | 4 ++-- src/View/EntityViewBuilder.php | 4 ++-- src/View/FieldViewBuilder.php | 4 ++-- src/View/ImageViewBuilder.php | 3 ++- src/View/RegionViewBuilder.php | 2 +- tests/src/Kernel/ImageViewBuilderTest.php | 22 +++++++++++++++++----- 8 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/TwigTweakExtension.php b/src/TwigTweakExtension.php index 3667cb9..4d7ad01 100644 --- a/src/TwigTweakExtension.php +++ b/src/TwigTweakExtension.php @@ -547,7 +547,7 @@ class TwigTweakExtension extends AbstractExtension { /** @var \Drupal\Core\Entity\Plugin\DataType\EntityAdapter $parent */ if ($parent = $object->getParent()) { CacheableMetadata::createFromRenderArray($build) - ->merge(CacheableMetadata::createFromObject($parent->getEntity())) + ->addCacheableDependency($parent->getEntity()) ->applyTo($build); } } diff --git a/src/View/BlockViewBuilder.php b/src/View/BlockViewBuilder.php index 940ee81..440d83d 100644 --- a/src/View/BlockViewBuilder.php +++ b/src/View/BlockViewBuilder.php @@ -162,8 +162,8 @@ class BlockViewBuilder { } CacheableMetadata::createFromRenderArray($build) - ->merge(CacheableMetadata::createFromObject($access)) - ->merge(CacheableMetadata::createFromObject($block_plugin)) + ->addCacheableDependency($access) + ->addCacheableDependency($block_plugin) ->applyTo($build); if (!isset($build['#cache']['keys'])) { diff --git a/src/View/EntityFormViewBuilder.php b/src/View/EntityFormViewBuilder.php index 92b0626..a8f485e 100644 --- a/src/View/EntityFormViewBuilder.php +++ b/src/View/EntityFormViewBuilder.php @@ -50,8 +50,8 @@ class EntityFormViewBuilder { } CacheableMetadata::createFromRenderArray($build) - ->merge(CacheableMetadata::createFromObject($entity)) - ->merge(CacheableMetadata::createFromObject($access)) + ->addCacheableDependency($access) + ->addCacheableDependency($entity) ->applyTo($build); return $build; diff --git a/src/View/EntityViewBuilder.php b/src/View/EntityViewBuilder.php index 0125768..ba447ba 100644 --- a/src/View/EntityViewBuilder.php +++ b/src/View/EntityViewBuilder.php @@ -38,8 +38,8 @@ class EntityViewBuilder { ->view($entity, $view_mode, $langcode); } CacheableMetadata::createFromRenderArray($build) - ->merge(CacheableMetadata::createFromObject($entity)) - ->merge(CacheableMetadata::createFromObject($access)) + ->addCacheableDependency($access) + ->addCacheableDependency($entity) ->applyTo($build); return $build; } diff --git a/src/View/FieldViewBuilder.php b/src/View/FieldViewBuilder.php index 2ff87b3..57cc259 100644 --- a/src/View/FieldViewBuilder.php +++ b/src/View/FieldViewBuilder.php @@ -64,8 +64,8 @@ class FieldViewBuilder { } CacheableMetadata::createFromRenderArray($build) - ->merge(CacheableMetadata::createFromObject($access)) - ->merge(CacheableMetadata::createFromObject($entity)) + ->addCacheableDependency($access) + ->addCacheableDependency($entity) ->applyTo($build); return $build; diff --git a/src/View/ImageViewBuilder.php b/src/View/ImageViewBuilder.php index c45124b..de1d6da 100644 --- a/src/View/ImageViewBuilder.php +++ b/src/View/ImageViewBuilder.php @@ -52,7 +52,8 @@ class ImageViewBuilder { } CacheableMetadata::createFromRenderArray($build) - ->merge(CacheableMetadata::createFromObject($access)) + ->addCacheableDependency($access) + ->addCacheableDependency($file) ->applyTo($build); return $build; diff --git a/src/View/RegionViewBuilder.php b/src/View/RegionViewBuilder.php index 78edb8e..d1fd36d 100644 --- a/src/View/RegionViewBuilder.php +++ b/src/View/RegionViewBuilder.php @@ -89,7 +89,7 @@ class RegionViewBuilder { /** @var \Drupal\block\BlockInterface[] $blocks */ foreach ($blocks as $id => $block) { $access = $block->access('view', NULL, TRUE); - $cache_metadata = $cache_metadata->merge(CacheableMetadata::createFromObject($access)); + $cache_metadata = $cache_metadata->addCacheableDependency($access); if ($access->isAllowed()) { $block_plugin = $block->getPlugin(); if ($block_plugin instanceof TitleBlockPluginInterface) { diff --git a/tests/src/Kernel/ImageViewBuilderTest.php b/tests/src/Kernel/ImageViewBuilderTest.php index a79f83f..9c611e3 100644 --- a/tests/src/Kernel/ImageViewBuilderTest.php +++ b/tests/src/Kernel/ImageViewBuilderTest.php @@ -76,7 +76,10 @@ final class ImageViewBuilderTest extends KernelTestBase { 'user', 'user.permissions', ], - 'tags' => ['tag_for_public://ocean.jpg'], + 'tags' => [ + 'file:1', + 'tag_for_public://ocean.jpg', + ], 'max-age' => 70, ], ]; @@ -95,7 +98,10 @@ final class ImageViewBuilderTest extends KernelTestBase { 'user', 'user.permissions', ], - 'tags' => ['tag_for_public://ocean.jpg'], + 'tags' => [ + 'file:1', + 'tag_for_public://ocean.jpg', + ], 'max-age' => 70, ], ]; @@ -114,7 +120,10 @@ final class ImageViewBuilderTest extends KernelTestBase { 'user', 'user.permissions', ], - 'tags' => ['tag_for_public://ocean.jpg'], + 'tags' => [ + 'file:1', + 'tag_for_public://ocean.jpg', + ], 'max-age' => 70, ], ]; @@ -126,7 +135,10 @@ final class ImageViewBuilderTest extends KernelTestBase { $expected_build = [ '#cache' => [ 'contexts' => ['user'], - 'tags' => ['tag_for_private://sea.jpg'], + 'tags' => [ + 'file:2', + 'tag_for_private://sea.jpg', + ], 'max-age' => 70, ], ]; @@ -141,7 +153,7 @@ final class ImageViewBuilderTest extends KernelTestBase { '#theme' => 'image', '#cache' => [ 'contexts' => [], - 'tags' => [], + 'tags' => ['file:2'], 'max-age' => Cache::PERMANENT, ], ];