Skip to content
Snippets Groups Projects
Commit a4e51c82 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2464877 by Wim Leers: Update RendererInterface::addDependency() to...

Issue #2464877 by Wim Leers: Update RendererInterface::addDependency() to accept *any* object, not only CachableDependencyInterface objects
parent 6435e2ee
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -109,18 +109,26 @@ public static function createFromRenderArray(array $build) { ...@@ -109,18 +109,26 @@ public static function createFromRenderArray(array $build) {
} }
/** /**
* Creates a bubbleable metadata object from a cacheable depended object. * Creates a bubbleable metadata object from a depended object.
* *
* @param \Drupal\Core\Cache\CacheableDependencyInterface $object * @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $object
* The object whose cacheability metadata to retrieve. * The object whose cacheability metadata to retrieve.
* *
* @return static * @return static
*/ */
public static function createFromObject(CacheableDependencyInterface $object) { public static function createFromObject($object) {
if ($object instanceof CacheableDependencyInterface) {
$meta = new static();
$meta->contexts = $object->getCacheContexts();
$meta->tags = $object->getCacheTags();
$meta->maxAge = $object->getCacheMaxAge();
return $meta;
}
// Objects that don't implement CacheableDependencyInterface must be assumed
// to be uncacheable, so set max-age 0.
$meta = new static(); $meta = new static();
$meta->contexts = $object->getCacheContexts(); $meta->maxAge = 0;
$meta->tags = $object->getCacheTags();
$meta->maxAge = $object->getCacheMaxAge();
return $meta; return $meta;
} }
......
...@@ -792,7 +792,7 @@ public static function mergeBubbleableMetadata(array $a, array $b) { ...@@ -792,7 +792,7 @@ public static function mergeBubbleableMetadata(array $a, array $b) {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function addDependency(array &$elements, CacheableDependencyInterface $dependency) { public function addDependency(array &$elements, $dependency) {
$meta_a = BubbleableMetadata::createFromRenderArray($elements); $meta_a = BubbleableMetadata::createFromRenderArray($elements);
$meta_b = BubbleableMetadata::createFromObject($dependency); $meta_b = BubbleableMetadata::createFromObject($dependency);
$meta_a->merge($meta_b)->applyTo($elements); $meta_a->merge($meta_b)->applyTo($elements);
......
...@@ -351,10 +351,10 @@ public static function mergeBubbleableMetadata(array $a, array $b); ...@@ -351,10 +351,10 @@ public static function mergeBubbleableMetadata(array $a, array $b);
* *
* @param array &$elements * @param array &$elements
* The render array to update. * The render array to update.
* @param \Drupal\Core\Cache\CacheableDependencyInterface $dependency * @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $dependency
* The dependency. * The dependency.
*/ */
public function addDependency(array &$elements, CacheableDependencyInterface $dependency); public function addDependency(array &$elements, $dependency);
/** /**
* Merges two attachments arrays (which live under the '#attached' key). * Merges two attachments arrays (which live under the '#attached' key).
......
...@@ -136,7 +136,7 @@ public function providerTestCreateFromRenderArray() { ...@@ -136,7 +136,7 @@ public function providerTestCreateFromRenderArray() {
* @covers ::createFromObject * @covers ::createFromObject
* @dataProvider providerTestCreateFromObject * @dataProvider providerTestCreateFromObject
*/ */
public function testCreateFromObject(CacheableDependencyInterface $object, BubbleableMetadata $expected) { public function testCreateFromObject($object, BubbleableMetadata $expected) {
$this->assertEquals($expected, BubbleableMetadata::createFromObject($object)); $this->assertEquals($expected, BubbleableMetadata::createFromObject($object));
} }
...@@ -153,12 +153,16 @@ public function providerTestCreateFromObject() { ...@@ -153,12 +153,16 @@ public function providerTestCreateFromObject() {
$nonempty_metadata->setCacheContexts(['qux']) $nonempty_metadata->setCacheContexts(['qux'])
->setCacheTags(['foo:bar']) ->setCacheTags(['foo:bar'])
->setCacheMaxAge(600); ->setCacheMaxAge(600);
$uncacheable_metadata = new BubbleableMetadata();
$uncacheable_metadata->setCacheMaxAge(0);
$empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT); $empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT);
$nonempty_cacheable_object = new TestCacheableDependency(['qux'], ['foo:bar'], 600); $nonempty_cacheable_object = new TestCacheableDependency(['qux'], ['foo:bar'], 600);
$uncacheable_object = new \stdClass();
$data[] = [$empty_cacheable_object, $empty_metadata]; $data[] = [$empty_cacheable_object, $empty_metadata];
$data[] = [$nonempty_cacheable_object, $nonempty_metadata]; $data[] = [$nonempty_cacheable_object, $nonempty_metadata];
$data[] = [$uncacheable_object, $uncacheable_metadata];
return $data; return $data;
} }
......
...@@ -626,7 +626,7 @@ public function providerTestRenderCacheMaxAge() { ...@@ -626,7 +626,7 @@ public function providerTestRenderCacheMaxAge() {
* *
* @dataProvider providerTestAddDependency * @dataProvider providerTestAddDependency
*/ */
public function testAddDependency(array $build, CacheableDependencyInterface $object, array $expected) { public function testAddDependency(array $build, $object, array $expected) {
$this->renderer->addDependency($build, $object); $this->renderer->addDependency($build, $object);
$this->assertEquals($build, $expected); $this->assertEquals($build, $expected);
} }
...@@ -681,6 +681,26 @@ public function providerTestAddDependency() { ...@@ -681,6 +681,26 @@ public function providerTestAddDependency() {
'#post_render_cache' => [], '#post_render_cache' => [],
], ],
], ],
// Cacheable render array, no cacheability.
[
[
'#cache' => [
'contexts' => ['theme'],
'tags' => ['bar'],
'max-age' => 600,
]
],
new \stdClass(),
[
'#cache' => [
'contexts' => ['theme'],
'tags' => ['bar'],
'max-age' => 0,
],
'#attached' => [],
'#post_render_cache' => [],
],
],
]; ];
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment