core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php | 13 +++++++++---- core/lib/Drupal/Core/Entity/EntityDisplayBase.php | 9 +++++++++ .../src/Plugin/Field/FieldFormatter/ImageFormatterBase.php | 5 +---- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php index accdd92..18fe5db 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Entity\Entity; use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Entity\Display\EntityViewDisplayInterface; use Drupal\Core\Entity\EntityDisplayPluginCollection; use Drupal\Core\Entity\EntityStorageInterface; @@ -246,10 +247,14 @@ 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]); + if ($field_access instanceof CacheableDependencyInterface) { + $this->renderer->addDependency($build_list[$id][$name], $field_access); + } + else { + BubbleableMetadata::createFromRenderArray($build_list[$id][$name]) + ->merge(BubbleableMetadata::createFromAccessResult($field_access)) + ->applyTo($build_list[$id][$name]); + } } } } 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/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); } }