Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Those formatters should add a cache tag, so that in case those files change, the caches are properly cleared.
It's not common in core, but when you add file_entity/media, then it is possible that files are changed and have fields that might affect the output.
Proposed resolution
Add the cache tags to all file and image formatters like this:
'#cache' => array(
'tags' => $item->entity->getCacheTags(),
),
Remaining tasks
User interface changes
API changes
Comment | File | Size | Author |
---|---|---|---|
#32 | interdiff-2388023-27-32.txt | 1.58 KB | GoZ |
#32 | core-file-image-formatters-cache-tags-2388023-32.patch | 8.86 KB | GoZ |
Comments
Comment #1
Wim LeersThanks, great catch. Should be easy to fix :)
Comment #2
Wim LeersComment #3
Wim LeersGoZ is working on this at Drupal Dev Days.
Comment #4
GoZ CreditAttribution: GoZ at Centarro commentedI add cache tags for image formatter with link.
I also change tests but can't test for the moment, they need to be reviewed and tested.
Comment #5
anavarreComment #7
Wim LeersProgress, yay! :) Thank you!
Let's use
File::load()
andImageStyle::load()
instead :)Do we really want to load the file every single time?
Comment #9
GoZ CreditAttribution: GoZ at Centarro commentedI fix some things with your recommendations.
I was disagree with previous tests, so new one aren't same as before.
Still one issue with tests, if i'm not wrong, cache tag should be found at line 159, but test fail because tag is not present (and should be). I think test is good, but point on a case where cache tag is missing.
Set needs review status to see l159 case. But still needs work
Comment #11
GoZ CreditAttribution: GoZ at Centarro commentedForget this case, i was wrong with test, cache tag should not be found since we search for file cache tag and do not display link.
Comment #13
GoZ CreditAttribution: GoZ at Centarro commentedComment #14
anavarre@GoZ could you please provide an interdiff with new patches? Thanks!
Comment #15
GoZ CreditAttribution: GoZ at Centarro commentedHere is interdiff.
Comment #16
anavarreComment #18
GoZ CreditAttribution: GoZ at Centarro commentedPatch can't apply since d8 has new commits.
Comment #19
Wim LeersLooking great! :) Test coverage is already sufficient, no need for more tests. Below, only nitpicks. The main missing thing is that file formatters still aren't setting File entity cache tags: this patch only deals with the Image formatter right now.
Why not move the
$file =
line to come first, and then do$image_uri = $file->getFileUri()
?I think we still want these assertions, don't we?
These assert that there are no image style cache tags at all. Just checking that there is no file cache tag either is good, but I don't see why we want to remove the existing assertions?
This change is correct, it's simple clean-up :) Can you remove the added newline though?
Yes, better! :)
Comment #20
yched CreditAttribution: yched commentedis this issue specific to image formatters ? Wouldn't any entity_ref formatter need to do something similar ?
Comment #21
GoZ CreditAttribution: GoZ at Centarro commented@Yched : This issue is only for file and image formatters. For some others, better is to have an issue by formatter to check.
Here is corrections based on Wim #19 comments plus file formatter.
Comment #22
yched CreditAttribution: yched commentedThe name is misleading, but that one is actually not a formatter for fields of type "file" (i.e fields referencing a File entiy), it's a helper base class for formatters of the base fields living *on* the File entity itself.
So this is not part of the issue here, and this hunk should be reverted :-)
Comment #23
GoZ CreditAttribution: GoZ at Centarro commentedHere is patch without adding cache tags and tests for BaseFieldFileFormatterBase
Comment #24
Wim LeersAwesome, thank you! :)
Committer: please remove this single line of unnecessary whitespace :)
Comment #25
yched CreditAttribution: yched commentedSorry, hadn't spotted that earlier :
This doesn't look correct : we should be adding the $file's tags unconditionally, not only within this "if (we're wrapping the output in a link)"
So that line should move just outside if the if branch (or it could also be directly inlined in the line that does '#tags' => ...)
Other than that, RTBC++ :-)
Comment #26
Wim LeersNW for that tiny bit. So very close!
Comment #27
GoZ CreditAttribution: GoZ at Centarro commentedLine has moved outside, and tests should now see cache tag each time file is displayed.
Comment #28
Wim LeersThose assertions also make 200% more sense. Can't believe I missed that before.
@yched++
@GoZ++
Comment #29
yched CreditAttribution: yched commentedRTBC +1, thanks @GoZ :-)
Comment #30
BerdirYou should actually be able to use $node->{$field_name}->entity here, if you're changing it anyway.
same here.
and here :)
But the changes look good to me as well. Next step would then be to update #2464475: Add cache tag test coverage for File content entity so that it uses a file reference instead of an entity reference and a file formatter and make sure it's still working (which it should now, with this fixed).
Comment #31
Wim Leers#30: ooh, nice!
Let's do that too.
Comment #32
GoZ CreditAttribution: GoZ at Centarro commentedHere patch for #30.
Comment #33
Wim LeersGreat, thanks!
Comment #34
yched CreditAttribution: yched commentedFacepalm. I was like "nice, this removes a file_load() call", but didn't look further. @Berdir++
Comment #35
alexpottThis issue addresses a major bug and is allowed per https://www.drupal.org/core/beta-changes. Committed 9def612 and pushed to 8.0.x. Thanks!