diff --git a/core/lib/Drupal/Core/Cache/CacheableMetadata.php b/core/lib/Drupal/Core/Cache/CacheableMetadata.php index a0c1ba8907c20c882cd607c9ce2ebbe41ee40bca..acb005c053cb6895c5a8bc478d6f272f619cf899 100644 --- a/core/lib/Drupal/Core/Cache/CacheableMetadata.php +++ b/core/lib/Drupal/Core/Cache/CacheableMetadata.php @@ -139,7 +139,7 @@ public function setCacheMaxAge($max_age) { * A new CacheableMetadata object, with the merged data. */ public function merge(CacheableMetadata $other) { - $result = new static(); + $result = clone $this; // This is called many times per request, so avoid merging unless absolutely // necessary. diff --git a/core/modules/filter/src/Tests/FilterAPITest.php b/core/modules/filter/src/Tests/FilterAPITest.php index ff23d39bb78fd29b0d68057f8cbac3ffe8c2afc0..4ad909b6a459eeef07613a4f33f9f6f35ea69b8e 100644 --- a/core/modules/filter/src/Tests/FilterAPITest.php +++ b/core/modules/filter/src/Tests/FilterAPITest.php @@ -221,6 +221,10 @@ function testProcessedTextElement() { 'weight' => 0, 'status' => TRUE, ), + 'filter_test_cache_merge' => array( + 'weight' => 0, + 'status' => TRUE, + ), 'filter_test_placeholders' => array( 'weight' => 1, 'status' => TRUE, @@ -257,6 +261,8 @@ function testProcessedTextElement() { // The cache tags set by the filter_test_cache_tags filter. 'foo:bar', 'foo:baz', + // The cache tags set by the filter_test_cache_merge filter. + 'merge:tag', ); $this->assertEqual($expected_cache_tags, $build['#cache']['tags'], 'Expected cache tags present.'); $expected_cache_contexts = [ @@ -265,6 +271,8 @@ function testProcessedTextElement() { // The default cache contexts for Renderer. 'languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', + // The cache tags set by the filter_test_cache_merge filter. + 'user.permissions', ]; $this->assertEqual($expected_cache_contexts, $build['#cache']['contexts'], 'Expected cache contexts present.'); $expected_markup = '
Hello, world!
This is a dynamic llama.
'; diff --git a/core/modules/filter/tests/filter_test/src/Plugin/Filter/FilterTestCacheMerge.php b/core/modules/filter/tests/filter_test/src/Plugin/Filter/FilterTestCacheMerge.php new file mode 100644 index 0000000000000000000000000000000000000000..9e56c9bf9d050021fc04515fc30739532968e823 --- /dev/null +++ b/core/modules/filter/tests/filter_test/src/Plugin/Filter/FilterTestCacheMerge.php @@ -0,0 +1,41 @@ +addCacheTags(['merge:tag']); + $metadata->addCacheContexts(['user.permissions']); + $result = $result->merge($metadata); + + return $result; + } + +}