Problem/Motivation
The non-lazy formatter uses addCacheableDependency($view) yet the addCacheableDependency() method bails as soon as it sees that the object does not implement CacheableDependencyInterface (which views does not).
Steps to reproduce
Use the non-lazy formatter and check the #cache on the render, it will always have max-age of 0. You can see this here.
Screenshot of the line that always gets hit now for convenience (but available in more accessible format via the link above):

Proposed resolution
Respect the #cache from the View by using CacheableMetadata to merge the View #cache onto the $element.
Remaining tasks
Merge request incoming
User interface changes
None
API changes
None
Data model changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | 2024-05-02_06-38.png | 55.27 KB | scott_euser |
Issue fork viewsreference-3444744
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
scott_euser commentedLooks like this regression was introduced in #3426640: Cache metadata is not captured when no results
Comment #4
scott_euser commentedComment #5
joekersThanks for raising and fixing the issue - merged into dev.
Comment #6
donquixote commentedI have not tested, but the change looks reasonable to me.
I assume/hope the render element will contain all the necessary cache metadata, even on empty result.
Comment #7
donquixote commentedSorry for changing the status!
Comment #8
scott_euser commentedYep it takes all attributes of the #cache key, in the same way that add cacheable dependency does so if the view returns a max age of zero like the original issue re no results, that will be carried across into the $elements render array.
Comment #9
scott_euser commentedThanks for actioning this so quickly by the way, much appreciated! Was a bit worried about some higher traffic sites taking a performance hit.
Comment #10
donquixote commented@scott_euser
Could we come up with a testing scenario to confirm that this works correctly on a given website?
Comment #11
scott_euser commentedYeah we would just render a view programmatically and check cacheability then again same view but via views reference field formatter. But this module has very little test coverage as it is, so really it's part of a much bigger job.