Problem/Motivation

See this excerpt from \Drupal\image\Plugin\Field\FieldFormatter\ImageFormatter::viewElements():

    $cache_tags = array();
    ...
    foreach ($files as $delta => $file) {
      ...
      $cache_tags = Cache::mergeTags($cache_tags, $file->getCacheTags());
    }

Cache tags from files that are later in the $files incorrectly get the cache tags from the earlier ones.

Proposed resolution

Change the variable name outside of the foreach loop to $base_cache_tags or (preferably) to something more semantic.

Remaining tasks

Write test to show that this is a problem.
Create patch that fixes the problem.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

tstoeckler created an issue. See original summary.

pcambra’s picture

Status: Active » Needs review
StatusFileSize
new2.31 KB
new1.19 KB

Just confirmed the error, I've added a test to verify it, not sure if where I've placed it is the best fit

Status: Needs review » Needs work

The last submitted patch, 2: 2743463-imageformatter-cache-tags-test.patch, failed testing.

pcambra’s picture

Status: Needs work » Needs review
mondrake’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me, has tests. Not sure do we need to clear caches?

tstoeckler’s picture

Status: Reviewed & tested by the community » Needs work

Sorry, X-post.

Yay, thanks for picking this up.

+      $cache_tags = $base_cache_tags;
...
       $cache_tags = Cache::mergeTags($cache_tags, $file->getCacheTags());

Sorry, for being a bit picky, but I think it would be nicer to just merge those lines. and do

      $cache_tags = Cache::mergeTags($base_cache_tags, $file->getCacheTags());

directly.
For the cache contexts it makes sense to declare the variable separately, because the context is only added conditionally, but this is not the case for the tags.

Also I think we should find a better place for the test. ImageItemTest actually tests the field functionality itself, and I think it would be not super nice to mix tests for the formatter in with it.

Sadly there's no ImageFormatterTest yet, but maybe we can create it (as another kernel test). The test code itself looks pretty great, in my opinion. The only quibble I have is that we could be a bit more explicit and actually instantiate the formatter and call the viewElements() method on it (or whichever it is).

pcambra’s picture

Status: Needs work » Needs review
StatusFileSize
new4.63 KB
new5.42 KB

Let's see if this looks better :)

tstoeckler’s picture

StatusFileSize
new3.05 KB

Looks great, thanks a lot for the quick follow-up. Uploading a tests only patch just to see if it still fails as expected, but absolutely RTBC if so.

tstoeckler’s picture

tstoeckler’s picture

StatusFileSize
new3.05 KB

Ahh, the testbot still can't run files which are uploaded as "hidden"...

Status: Needs review » Needs work

The last submitted patch, 10: 2743463-imageformatter-cache-tags-7--tests-only.patch, failed testing.

tstoeckler’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new4.63 KB

Awesome! Re-uploading #7 and marking RTBC.

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.2.x and cherry-picked to 8.1.x. Thanks!

  • catch committed e5423fd on 8.2.x
    Issue #2743463 by pcambra, tstoeckler: ImageFormatter calculates cache...

  • catch committed d0154aa on 8.1.x
    Issue #2743463 by pcambra, tstoeckler: ImageFormatter calculates cache...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.