core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php | 6 ++---- core/lib/Drupal/Core/Entity/EntityDisplayBase.php | 9 +++++++++ core/lib/Drupal/Core/Render/Renderer.php | 10 ++++++++++ core/lib/Drupal/Core/Render/RendererInterface.php | 3 +++ .../src/Plugin/Field/FieldFormatter/ImageFormatterBase.php | 5 +---- 5 files changed, 25 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..b7d4932 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,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->addAccessResult($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/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php index 0ec8fa1..d52b252 100644 --- a/core/lib/Drupal/Core/Render/Renderer.php +++ b/core/lib/Drupal/Core/Render/Renderer.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Access\AccessResultInterface; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Cache\CacheContexts; @@ -801,6 +802,15 @@ public function addDependency(array &$elements, CacheableDependencyInterface $de /** * {@inheritdoc} */ + public function addAccessResult(array &$elements, AccessResultInterface $access_result) { + $meta_a = BubbleableMetadata::createFromRenderArray($elements); + $meta_b = BubbleableMetadata::createFromAccessResult($access_result); + $meta_a->merge($meta_b)->applyTo($elements); + } + + /** + * {@inheritdoc} + */ public static function mergeAttachments(array $a, array $b) { // If both #attached arrays contain drupalSettings, then merge them // correctly; adding the same settings multiple times needs to behave diff --git a/core/lib/Drupal/Core/Render/RendererInterface.php b/core/lib/Drupal/Core/Render/RendererInterface.php index 85035bf..8724b09 100644 --- a/core/lib/Drupal/Core/Render/RendererInterface.php +++ b/core/lib/Drupal/Core/Render/RendererInterface.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Render; +use Drupal\Core\Access\AccessResultInterface; use Drupal\Core\Cache\CacheableDependencyInterface; /** @@ -356,6 +357,8 @@ public static function mergeBubbleableMetadata(array $a, array $b); */ public function addDependency(array &$elements, CacheableDependencyInterface $dependency); + public function addAccessResult(array &$elements, AccessResultInterface $access_result); + /** * 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); } }