Commit 9def6124 authored by alexpott's avatar alexpott

Issue #2388023 by GoZ: File/Image field formatters don't add a cache tag for the file they display

parent be15ea2b
...@@ -34,6 +34,9 @@ public function viewElements(FieldItemListInterface $items) { ...@@ -34,6 +34,9 @@ public function viewElements(FieldItemListInterface $items) {
'#theme' => 'file_link', '#theme' => 'file_link',
'#file' => $file, '#file' => $file,
'#description' => $item->description, '#description' => $item->description,
'#cache' => array(
'tags' => $file->getCacheTags(),
),
); );
// Pass field item attributes to the theme function. // Pass field item attributes to the theme function.
if (isset($item->_attributes)) { if (isset($item->_attributes)) {
......
...@@ -37,6 +37,9 @@ public function viewElements(FieldItemListInterface $items) { ...@@ -37,6 +37,9 @@ public function viewElements(FieldItemListInterface $items) {
'data' => array( 'data' => array(
'#theme' => 'file_link', '#theme' => 'file_link',
'#file' => $file, '#file' => $file,
'#cache' => array(
'tags' => $file->getCacheTags(),
),
), ),
), ),
array('data' => format_size($file->getSize())), array('data' => format_size($file->getSize())),
......
...@@ -29,7 +29,12 @@ public function viewElements(FieldItemListInterface $items) { ...@@ -29,7 +29,12 @@ public function viewElements(FieldItemListInterface $items) {
$elements = array(); $elements = array();
foreach ($this->getEntitiesToView($items) as $delta => $file) { foreach ($this->getEntitiesToView($items) as $delta => $file) {
$elements[$delta] = array('#markup' => file_create_url($file->getFileUri())); $elements[$delta] = array(
'#markup' => file_create_url($file->getFileUri()),
'#cache' => array(
'tags' => $file->getCacheTags(),
),
);
} }
return $elements; return $elements;
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
use Drupal\Core\Utility\LinkGeneratorInterface; use Drupal\Core\Utility\LinkGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
/** /**
* Plugin implementation of the 'image' formatter. * Plugin implementation of the 'image' formatter.
...@@ -211,6 +212,7 @@ public function viewElements(FieldItemListInterface $items) { ...@@ -211,6 +212,7 @@ public function viewElements(FieldItemListInterface $items) {
$image_uri = $file->getFileUri(); $image_uri = $file->getFileUri();
$url = Url::fromUri(file_create_url($image_uri)); $url = Url::fromUri(file_create_url($image_uri));
} }
$cache_tags = Cache::mergeTags($cache_tags, $file->getCacheTags());
// Extract field item attributes for the theme function, and unset them // Extract field item attributes for the theme function, and unset them
// from the $item so that the field template does not re-render them. // from the $item so that the field template does not re-render them.
......
...@@ -94,7 +94,8 @@ function _testImageFieldFormatters($scheme) { ...@@ -94,7 +94,8 @@ function _testImageFieldFormatters($scheme) {
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
// Test that the default formatter is being used. // Test that the default formatter is being used.
$image_uri = file_load($node->{$field_name}->target_id)->getFileUri(); $file = $node->{$field_name}->entity;
$image_uri = $file->getFileUri();
$image = array( $image = array(
'#theme' => 'image', '#theme' => 'image',
'#uri' => $image_uri, '#uri' => $image_uri,
...@@ -123,6 +124,7 @@ function _testImageFieldFormatters($scheme) { ...@@ -123,6 +124,7 @@ function _testImageFieldFormatters($scheme) {
); );
$default_output = '<a href="' . file_create_url($image_uri) . '">' . drupal_render($image) . '</a>'; $default_output = '<a href="' . file_create_url($image_uri) . '">' . drupal_render($image) . '</a>';
$this->drupalGet('node/' . $nid); $this->drupalGet('node/' . $nid);
$this->assertCacheTag($file->getCacheTags()[0]);
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
$this->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.'); $this->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.');
...@@ -154,6 +156,7 @@ function _testImageFieldFormatters($scheme) { ...@@ -154,6 +156,7 @@ function _testImageFieldFormatters($scheme) {
'#height' => 20, '#height' => 20,
); );
$this->drupalGet('node/' . $nid); $this->drupalGet('node/' . $nid);
$this->assertCacheTag($file->getCacheTags()[0]);
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
$elements = $this->xpath( $elements = $this->xpath(
...@@ -187,8 +190,8 @@ function _testImageFieldFormatters($scheme) { ...@@ -187,8 +190,8 @@ function _testImageFieldFormatters($scheme) {
); );
$default_output = drupal_render($image_style); $default_output = drupal_render($image_style);
$this->drupalGet('node/' . $nid); $this->drupalGet('node/' . $nid);
$cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags')); $image_style = ImageStyle::load('thumbnail');
$this->assertTrue(in_array('config:image.style.thumbnail', $cache_tags)); $this->assertCacheTag($image_style->getCacheTags()[0]);
$this->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.'); $this->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.');
if ($scheme == 'private') { if ($scheme == 'private') {
...@@ -247,9 +250,10 @@ function testImageFieldSettings() { ...@@ -247,9 +250,10 @@ function testImageFieldSettings() {
// style. // style.
$node_storage->resetCache(array($nid)); $node_storage->resetCache(array($nid));
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
$file = $node->{$field_name}->entity;
$image_style = array( $image_style = array(
'#theme' => 'image_style', '#theme' => 'image_style',
'#uri' => file_load($node->{$field_name}->target_id)->getFileUri(), '#uri' => $file->getFileUri(),
'#width' => 40, '#width' => 40,
'#height' => 20, '#height' => 20,
'#style_name' => 'medium', '#style_name' => 'medium',
...@@ -260,7 +264,7 @@ function testImageFieldSettings() { ...@@ -260,7 +264,7 @@ function testImageFieldSettings() {
// Add alt/title fields to the image and verify that they are displayed. // Add alt/title fields to the image and verify that they are displayed.
$image = array( $image = array(
'#theme' => 'image', '#theme' => 'image',
'#uri' => file_load($node->{$field_name}->target_id)->getFileUri(), '#uri' => $file->getFileUri(),
'#alt' => $alt, '#alt' => $alt,
'#title' => $this->randomMachineName(), '#title' => $this->randomMachineName(),
'#width' => 40, '#width' => 40,
...@@ -362,6 +366,7 @@ function testImageFieldDefaultImage() { ...@@ -362,6 +366,7 @@ function testImageFieldDefaultImage() {
); );
$default_output = str_replace("\n", NULL, drupal_render($image)); $default_output = str_replace("\n", NULL, drupal_render($image));
$this->drupalGet('node/' . $node->id()); $this->drupalGet('node/' . $node->id());
$this->assertCacheTag($file->getCacheTags()[0]);
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
$this->assertRaw($default_output, 'Default image displayed when no user supplied image is present.'); $this->assertRaw($default_output, 'Default image displayed when no user supplied image is present.');
...@@ -375,15 +380,17 @@ function testImageFieldDefaultImage() { ...@@ -375,15 +380,17 @@ function testImageFieldDefaultImage() {
$nid = $this->uploadNodeImage($images[1], $field_name, 'article', $alt); $nid = $this->uploadNodeImage($images[1], $field_name, 'article', $alt);
$node_storage->resetCache(array($nid)); $node_storage->resetCache(array($nid));
$node = $node_storage->load($nid); $node = $node_storage->load($nid);
$file = $node->{$field_name}->entity;
$image = array( $image = array(
'#theme' => 'image', '#theme' => 'image',
'#uri' => file_load($node->{$field_name}->target_id)->getFileUri(), '#uri' => $file->getFileUri(),
'#width' => 40, '#width' => 40,
'#height' => 20, '#height' => 20,
'#alt' => $alt, '#alt' => $alt,
); );
$image_output = str_replace("\n", NULL, drupal_render($image)); $image_output = str_replace("\n", NULL, drupal_render($image));
$this->drupalGet('node/' . $nid); $this->drupalGet('node/' . $nid);
$this->assertCacheTag($file->getCacheTags()[0]);
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
$this->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.'); $this->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.');
...@@ -431,6 +438,7 @@ function testImageFieldDefaultImage() { ...@@ -431,6 +438,7 @@ function testImageFieldDefaultImage() {
); );
$default_output = str_replace("\n", NULL, drupal_render($image)); $default_output = str_replace("\n", NULL, drupal_render($image));
$this->drupalGet('node/' . $node->id()); $this->drupalGet('node/' . $node->id());
$this->assertCacheTag($file->getCacheTags()[0]);
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
$this->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.'); $this->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.');
......
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