Commit a4e51c82 authored by alexpott's avatar alexpott

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
......@@ -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.
*
* @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->contexts = $object->getCacheContexts();
$meta->tags = $object->getCacheTags();
$meta->maxAge = $object->getCacheMaxAge();
$meta->maxAge = 0;
return $meta;
}
......
......@@ -792,7 +792,7 @@ public static function mergeBubbleableMetadata(array $a, array $b) {
/**
* {@inheritdoc}
*/
public function addDependency(array &$elements, CacheableDependencyInterface $dependency) {
public function addDependency(array &$elements, $dependency) {
$meta_a = BubbleableMetadata::createFromRenderArray($elements);
$meta_b = BubbleableMetadata::createFromObject($dependency);
$meta_a->merge($meta_b)->applyTo($elements);
......
......@@ -351,10 +351,10 @@ public static function mergeBubbleableMetadata(array $a, array $b);
*
* @param array &$elements
* The render array to update.
* @param \Drupal\Core\Cache\CacheableDependencyInterface $dependency
* @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $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).
......
......@@ -136,7 +136,7 @@ public function providerTestCreateFromRenderArray() {
* @covers ::createFromObject
* @dataProvider providerTestCreateFromObject
*/
public function testCreateFromObject(CacheableDependencyInterface $object, BubbleableMetadata $expected) {
public function testCreateFromObject($object, BubbleableMetadata $expected) {
$this->assertEquals($expected, BubbleableMetadata::createFromObject($object));
}
......@@ -153,12 +153,16 @@ public function providerTestCreateFromObject() {
$nonempty_metadata->setCacheContexts(['qux'])
->setCacheTags(['foo:bar'])
->setCacheMaxAge(600);
$uncacheable_metadata = new BubbleableMetadata();
$uncacheable_metadata->setCacheMaxAge(0);
$empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT);
$nonempty_cacheable_object = new TestCacheableDependency(['qux'], ['foo:bar'], 600);
$uncacheable_object = new \stdClass();
$data[] = [$empty_cacheable_object, $empty_metadata];
$data[] = [$nonempty_cacheable_object, $nonempty_metadata];
$data[] = [$uncacheable_object, $uncacheable_metadata];
return $data;
}
......
......@@ -626,7 +626,7 @@ public function providerTestRenderCacheMaxAge() {
*
* @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->assertEquals($build, $expected);
}
......@@ -681,6 +681,26 @@ public function providerTestAddDependency() {
'#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' => [],
],
],
];
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment