diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/BaseFieldFileFormatterBase.php b/core/modules/file/src/Plugin/Field/FieldFormatter/BaseFieldFileFormatterBase.php index 8654712..f42baa3 100644 --- a/core/modules/file/src/Plugin/Field/FieldFormatter/BaseFieldFileFormatterBase.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/BaseFieldFileFormatterBase.php @@ -63,6 +63,9 @@ public function viewElements(FieldItemListInterface $items) { '#type' => 'link', '#title' => $string, '#url' => Url::fromUri($url), + '#cache' => array( + 'tags' => $items->getEntity()->getCacheTags(), + ), ]; } else { diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php index cb917eb..f370037 100644 --- a/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/GenericFileFormatter.php @@ -34,6 +34,9 @@ public function viewElements(FieldItemListInterface $items) { '#theme' => 'file_link', '#file' => $file, '#description' => $item->description, + '#cache' => array( + 'tags' => $file->getCacheTags(), + ), ); // Pass field item attributes to the theme function. if (isset($item->_attributes)) { diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php index 460450d..9bf55ee 100644 --- a/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/TableFormatter.php @@ -37,6 +37,9 @@ public function viewElements(FieldItemListInterface $items) { 'data' => array( '#theme' => 'file_link', '#file' => $file, + '#cache' => array( + 'tags' => $file->getCacheTags(), + ), ), ), array('data' => format_size($file->getSize())), diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php index 26b78f7..f61a98d 100644 --- a/core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/UrlPlainFormatter.php @@ -29,7 +29,12 @@ public function viewElements(FieldItemListInterface $items) { $elements = array(); 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; diff --git a/core/modules/file/src/Tests/Formatter/FileEntityFormatterTest.php b/core/modules/file/src/Tests/Formatter/FileEntityFormatterTest.php index 1c9a275..06d1997 100644 --- a/core/modules/file/src/Tests/Formatter/FileEntityFormatterTest.php +++ b/core/modules/file/src/Tests/Formatter/FileEntityFormatterTest.php @@ -84,6 +84,7 @@ public function testFormatterFileLink() { $entity_display->setComponent('filename', ['type' => 'file_link']); $build = $entity_display->buildMultiple($this->files)[0]['filename'][0]; + $this->assertTrue(in_array($this->files[0]->getCacheTags()[0], $build['#cache']['tags']), 'File cache tag found'); $this->assertEqual('file.png', $build['#title']); $this->assertEqual(Url::fromUri(file_create_url('public://file.png')), $build['#url']); } diff --git a/core/modules/image/src/Tests/ImageFieldDisplayTest.php b/core/modules/image/src/Tests/ImageFieldDisplayTest.php index 0991d6a..de41118 100644 --- a/core/modules/image/src/Tests/ImageFieldDisplayTest.php +++ b/core/modules/image/src/Tests/ImageFieldDisplayTest.php @@ -95,7 +95,8 @@ function _testImageFieldFormatters($scheme) { $node = $node_storage->load($nid); // Test that the default formatter is being used. - $image_uri = File::load($node->{$field_name}->target_id)->getFileUri(); + $file = File::load($node->{$field_name}->target_id); + $image_uri = $file->getFileUri(); $image = array( '#theme' => 'image', '#uri' => $image_uri, @@ -125,8 +126,9 @@ function _testImageFieldFormatters($scheme) { $default_output = '' . drupal_render($image) . ''; $this->drupalGet('node/' . $nid); - $file = File::load($node->{$field_name}->target_id); $this->assertCacheTag($file->getCacheTags()[0]); + $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->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.'); // Verify that the image can be downloaded. $this->assertEqual(file_get_contents($test_image->uri), $this->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.'); @@ -157,6 +159,8 @@ function _testImageFieldFormatters($scheme) { ); $this->drupalGet('node/' . $nid); $this->assertNoCacheTag($file->getCacheTags()[0]); + $cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags'); + $this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.'); $elements = $this->xpath( '//a[@href=:path]/img[@src=:url and @alt=:alt and @width=:width and @height=:height]', array( @@ -188,7 +192,6 @@ function _testImageFieldFormatters($scheme) { ); $default_output = drupal_render($image_style); $this->drupalGet('node/' . $nid); - $image_style = ImageStyle::load('thumbnail'); $this->assertCacheTag($image_style->getCacheTags()[0]); $this->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.'); @@ -366,6 +369,8 @@ function testImageFieldDefaultImage() { $default_output = str_replace("\n", NULL, drupal_render($image)); $this->drupalGet('node/' . $node->id()); $this->assertNoCacheTag($file->getCacheTags()[0]); + $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->assertRaw($default_output, 'Default image displayed when no user supplied image is present.'); // Create a node with an image attached and ensure that the default image @@ -388,6 +393,8 @@ function testImageFieldDefaultImage() { $image_output = str_replace("\n", NULL, drupal_render($image)); $this->drupalGet('node/' . $nid); $this->assertNoCacheTag($file->getCacheTags()[0]); + $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->assertNoRaw($default_output, 'Default image is not displayed when user supplied image is present.'); $this->assertRaw($image_output, 'User supplied image is displayed.'); @@ -434,6 +441,8 @@ function testImageFieldDefaultImage() { $default_output = str_replace("\n", NULL, drupal_render($image)); $this->drupalGet('node/' . $node->id()); $this->assertNoCacheTag($file->getCacheTags()[0]); + $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->assertRaw($default_output, 'Default private image displayed when no user supplied image is present.'); }