Commit 5df2dddc authored by catch's avatar catch

Issue #2854454 by Maouna, hchonov: EntityReferenceFormatterBase::view() is...

Issue #2854454 by Maouna, hchonov: EntityReferenceFormatterBase::view() is applying the cacheability metadata but it should be merging it in order to preserve already present cacheability metadata
parent facaf224
......@@ -104,7 +104,8 @@ public function view(FieldItemListInterface $items, $langcode = NULL) {
// tags on which the access results depend, to ensure users that cannot view
// this field at the moment will gain access once any of those cache tags
// are invalidated.
$field_level_access_cacheability->applyTo($elements);
$field_level_access_cacheability->merge(CacheableMetadata::createFromRenderArray($elements))
->applyTo($elements);
return $elements;
}
......
......@@ -163,6 +163,19 @@ public function testAccess() {
}
}
/**
* Tests the merging of cache metadata.
*/
public function testCustomCacheTagFormatter() {
/** @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = $this->container->get('renderer');
$formatter = 'entity_reference_custom_cache_tag';
$build = $this->buildRenderArray([$this->referencedEntity], $formatter);
$renderer->renderRoot($build);
$this->assertTrue(in_array('custom_cache_tag', $build['#cache']['tags']));
}
/**
* Tests the ID formatter.
*/
......
<?php
namespace Drupal\entity_test\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
/**
* Plugin implementation of the 'entity_reference_custom_cache_tag' formatter.
*
* @FieldFormatter(
* id = "entity_reference_custom_cache_tag",
* label = @Translation("Custom cache tag"),
* field_types = {
* "entity_reference"
* }
* )
*/
class EntityTestReferenceCustomCacheTagFormatter extends EntityReferenceFormatterBase {
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
return ['#cache' => ['tags' => ['custom_cache_tag']]];
}
}
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