.../Core/Entity/Entity/EntityViewDisplay.php | 6 +--- core/lib/Drupal/Core/Entity/EntityDisplayBase.php | 9 ++++++ .../EntityReferenceFormatterBase.php | 2 +- core/lib/Drupal/Core/Render/BubbleableMetadata.php | 34 +++++++--------------- core/lib/Drupal/Core/Render/Renderer.php | 2 +- core/lib/Drupal/Core/Render/RendererInterface.php | 4 +-- .../Field/FieldFormatter/ImageFormatterBase.php | 5 +--- .../Tests/Core/Render/BubbleableMetadataTest.php | 6 +++- .../Drupal/Tests/Core/Render/RendererTest.php | 22 +++++++++++++- 9 files changed, 52 insertions(+), 38 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php index accdd92..8a724a3 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php @@ -13,7 +13,6 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Entity\EntityDisplayBase; -use Drupal\Core\Render\BubbleableMetadata; /** * Configuration entity that contains display options for all components of a @@ -246,10 +245,7 @@ public function buildMultiple(array $entities) { $build_list[$id][$name] = $formatter->view($items); } // Apply the field access cacheability metadata to the render array. - // @todo Use RendererInterface::addDependency() when https://www.drupal.org/node/2444231 lands - BubbleableMetadata::createFromRenderArray($build_list[$id][$name]) - ->merge(BubbleableMetadata::createFromAccessResult($field_access)) - ->applyTo($build_list[$id][$name]); + $this->renderer->addDependency($build_list[$id][$name], $field_access); } } } diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php index 96fa0de..ce1740f 100644 --- a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php +++ b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php @@ -115,6 +115,13 @@ protected $pluginManager; /** + * The renderer. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + + /** * {@inheritdoc} */ public function __construct(array $values, $entity_type) { @@ -126,6 +133,8 @@ public function __construct(array $values, $entity_type) { throw new \InvalidArgumentException('EntityDisplay entities can only handle fieldable entity types.'); } + $this->renderer = \Drupal::service('renderer'); + // A plugin manager and a context type needs to be set by extending classes. if (!isset($this->pluginManager)) { throw new \RuntimeException('Missing plugin manager.'); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php index 264bedf..0a5b159 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceFormatterBase.php @@ -59,7 +59,7 @@ protected function getEntitiesToView(array &$build, EntityReferenceFieldItemList } $access = $this->checkAccess($item); - $access_cacheability = $access_cacheability->merge(BubbleableMetadata::createFromAccessResult($access)); + $access_cacheability = $access_cacheability->merge(BubbleableMetadata::createFromObject($access)); if ($access->isAllowed()) { // Add the referring item, in case the formatter needs it. $entity->_referringItem = $items[$delta]; diff --git a/core/lib/Drupal/Core/Render/BubbleableMetadata.php b/core/lib/Drupal/Core/Render/BubbleableMetadata.php index 82324da..1bbd8ab 100644 --- a/core/lib/Drupal/Core/Render/BubbleableMetadata.php +++ b/core/lib/Drupal/Core/Render/BubbleableMetadata.php @@ -110,36 +110,24 @@ 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) { - $meta = new static(); - $meta->contexts = $object->getCacheContexts(); - $meta->tags = $object->getCacheTags(); - $meta->maxAge = $object->getCacheMaxAge(); - return $meta; - } - - /** - * Creates a bubbleable metadata object from an access result. - * - * @param \Drupal\Core\Access\AccessResultInterface $access_result - * An access result. - * - * @return static - */ - public static function createFromAccessResult(AccessResultInterface $access_result) { - if ($access_result instanceof CacheableDependencyInterface) { - return static::createFromObject($access_result); + 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; } - // Any AccessResultInterface object that doesn't implement - // CacheableDependencyInterface must be assumed to be uncacheable. + // Objects that don't implement CacheableDependencyInterface must be assumed + // to be uncacheable, so set max-age 0. $meta = new static(); $meta->maxAge = 0; return $meta; diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php index 0ec8fa1..1c45716 100644 --- a/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -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); diff --git a/core/lib/Drupal/Core/Render/RendererInterface.php b/core/lib/Drupal/Core/Render/RendererInterface.php index 85035bf..da8d567 100644 --- a/core/lib/Drupal/Core/Render/RendererInterface.php +++ b/core/lib/Drupal/Core/Render/RendererInterface.php @@ -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). diff --git a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php index dad8275..e637307 100644 --- a/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php +++ b/core/modules/image/src/Plugin/Field/FieldFormatter/ImageFormatterBase.php @@ -50,10 +50,7 @@ protected function getEntitiesToView(array &$build, EntityReferenceFieldItemList // Add the fallback image's cache tags to the render array, so that if // the fallback image is modified, all render cached fields displaying // the fallback image are updated. - // @todo Use RendererInterface::addDependency() when https://www.drupal.org/node/2444231 lands - BubbleableMetadata::createFromRenderArray($build) - ->merge(new BubbleableMetadata([], $file->getCacheTags())) - ->applyTo($build); + \Drupal::service('renderer')->addDependency($build, $file); } } diff --git a/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php b/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php index 526b7ee..721d992 100644 --- a/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php +++ b/core/tests/Drupal/Tests/Core/Render/BubbleableMetadataTest.php @@ -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; } diff --git a/core/tests/Drupal/Tests/Core/Render/RendererTest.php b/core/tests/Drupal/Tests/Core/Render/RendererTest.php index fb757f7..57272f8 100644 --- a/core/tests/Drupal/Tests/Core/Render/RendererTest.php +++ b/core/tests/Drupal/Tests/Core/Render/RendererTest.php @@ -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' => [], + ], + ], ]; }