Commit 677b9fc4 authored by catch's avatar catch

Issue #2204159 by Wim Leers, longwave, tim.plunkett: (Responsive) Image styles...

Issue #2204159 by Wim Leers, longwave, tim.plunkett: (Responsive) Image styles do not add correct cache tags, nor do they invalidate cache tags upon flushing.
parent 0b5a8cac
......@@ -7,6 +7,7 @@
namespace Drupal\image\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Config\Entity\EntityWithPluginBagInterface;
use Drupal\Core\Entity\EntityStorageInterface;
......@@ -262,8 +263,7 @@ public function flush($path = NULL) {
field_info_cache_clear();
drupal_theme_rebuild();
// Clear render cache when flushing.
\Drupal::cache('render')->deleteAll();
Cache::invalidateTags($this->getCacheTag());
return $this;
}
......
......@@ -113,6 +113,14 @@ public function viewElements(FieldItemListInterface $items) {
}
$image_style_setting = $this->getSetting('image_style');
// Collect cache tags to be added for each item in the field.
$cache_tags = array();
if (!empty($image_style_setting)) {
$image_style = entity_load('image_style', $image_style_setting);
$cache_tags = $image_style->getCacheTag();
}
foreach ($items as $delta => $item) {
if ($item->entity) {
if (isset($link_file)) {
......@@ -134,6 +142,9 @@ public function viewElements(FieldItemListInterface $items) {
'#item_attributes' => $item_attributes,
'#image_style' => $image_style_setting,
'#path' => isset($uri) ? $uri : '',
'#cache' => array(
'tags' => $cache_tags,
),
);
}
}
......
......@@ -14,6 +14,8 @@
*/
class ImageFieldDisplayTest extends ImageFieldTestBase {
protected $dumpHeaders = TRUE;
/**
* Modules to enable.
*
......@@ -90,6 +92,8 @@ function _testImageFieldFormatters($scheme) {
);
$default_output = l($image, file_create_url($image_uri), array('html' => TRUE));
$this->drupalGet('node/' . $nid);
$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.');
......@@ -120,6 +124,8 @@ function _testImageFieldFormatters($scheme) {
);
$default_output = l($image, 'node/' . $nid, array('html' => TRUE));
$this->drupalGet('node/' . $nid);
$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 content formatter displaying correctly on full node view.');
// Test the image style 'thumbnail' formatter.
......@@ -140,6 +146,8 @@ function _testImageFieldFormatters($scheme) {
);
$default_output = drupal_render($image_style);
$this->drupalGet('node/' . $nid);
$cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags'));
$this->assertTrue(in_array('image_style:thumbnail', $cache_tags));
$this->assertRaw($default_output, 'Image style thumbnail formatter displaying correctly on full node view.');
if ($scheme == 'private') {
......@@ -258,6 +266,8 @@ function testImageFieldDefaultImage() {
// Verify that no image is displayed on the page by checking for the class
// that would be used on the image field.
$this->assertNoPattern('<div class="(.*?)field-name-' . strtr($field_name, '_', '-') . '(.*?)">', 'No image displayed when no image is attached and no default image specified.');
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
// Add a default image to the public imagefield instance.
$images = $this->drupalGetTestFiles('image');
......@@ -285,6 +295,8 @@ function testImageFieldDefaultImage() {
);
$default_output = drupal_render($image);
$this->drupalGet('node/' . $node->id());
$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
......@@ -299,6 +311,8 @@ function testImageFieldDefaultImage() {
);
$image_output = drupal_render($image);
$this->drupalGet('node/' . $nid);
$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.');
......@@ -344,6 +358,9 @@ function testImageFieldDefaultImage() {
);
$default_output = drupal_render($image);
$this->drupalGet('node/' . $node->id());
$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.');
}
}
......@@ -7,6 +7,7 @@
namespace Drupal\responsive_image\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\image\Plugin\Field\FieldFormatter\ImageFormatterBase;
......@@ -168,6 +169,23 @@ public function viewElements(FieldItemListInterface $items) {
$fallback_image_style = $this->getSetting('fallback_image_style');
}
// Collect cache tags to be added for each item in the field.
$all_cache_tags = array();
if ($responsive_image_mapping) {
$all_cache_tags[] = $responsive_image_mapping->getCacheTag();
foreach ($breakpoint_styles as $breakpoint_name => $style_per_multiplier) {
foreach ($style_per_multiplier as $multiplier => $image_style_name) {
$image_style = entity_load('image_style', $image_style_name);
$all_cache_tags[] = $image_style->getCacheTag();
}
}
}
if ($fallback_image_style) {
$image_style = entity_load('image_style', $fallback_image_style);
$all_cache_tags[] = $image_style->getCacheTag();
}
$cache_tags = NestedArray::mergeDeepArray($all_cache_tags);
foreach ($items as $delta => $item) {
if (isset($link_file)) {
$uri = array(
......@@ -177,13 +195,18 @@ public function viewElements(FieldItemListInterface $items) {
}
$elements[$delta] = array(
'#theme' => 'responsive_image_formatter',
'#attached' => array('library' => array(
'core/picturefill',
)),
'#attached' => array(
'library' => array(
'core/picturefill',
)
),
'#item' => $item,
'#image_style' => $fallback_image_style,
'#breakpoints' => $breakpoint_styles,
'#path' => isset($uri) ? $uri : '',
'#cache' => array(
'tags' => $cache_tags,
)
);
}
......
......@@ -16,6 +16,8 @@
*/
class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
protected $dumpHeaders = TRUE;
/**
* Modules to enable.
*
......@@ -152,6 +154,9 @@ public function _testResponsiveImageFieldFormatters($scheme) {
);
$default_output = l($image, file_create_url($image_uri), array('html' => TRUE));
$this->drupalGet('node/' . $nid);
$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.');
......@@ -186,6 +191,11 @@ public function _testResponsiveImageFieldFormatters($scheme) {
$this->assertRaw('media="(min-width: 200px)"');
$this->assertRaw('media="(min-width: 400px)"');
$this->assertRaw('media="(min-width: 600px)"');
$cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags'));
$this->assertTrue(in_array('responsive_image_mapping:mapping_one', $cache_tags));
$this->assertTrue(in_array('image_style:thumbnail', $cache_tags));
$this->assertTrue(in_array('image_style:medium', $cache_tags));
$this->assertTrue(in_array('image_style:large', $cache_tags));
// Test the fallback image style.
$large_style = entity_load('image_style', 'large');
......@@ -202,6 +212,8 @@ public function _testResponsiveImageFieldFormatters($scheme) {
$this->drupalLogout();
$this->drupalGet($large_style->buildUrl($image_uri));
$this->assertResponse('403', 'Access denied to image style thumbnail as anonymous user.');
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
}
}
......
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