Issue #2464877 by Wim Leers: Update RendererInterface::addDependency() to accept *any* object, not only CachableDependencyInterface objects
parent
6435e2ee5d
commit
a4e51c82e6
|
@ -109,18 +109,26 @@ class BubbleableMetadata implements CacheableDependencyInterface {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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 @@ class Renderer implements RendererInterface {
|
|||
/**
|
||||
* {@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 @@ interface RendererInterface {
|
|||
*
|
||||
* @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 @@ class BubbleableMetadataTest extends UnitTestCase {
|
|||
* @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 @@ class BubbleableMetadataTest extends UnitTestCase {
|
|||
$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 @@ class RendererTest extends RendererTestBase {
|
|||
*
|
||||
* @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 @@ class RendererTest extends RendererTestBase {
|
|||
'#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' => [],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue