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

CommentFileSizeAuthor
#4 2024-05-02_06-38.png55.27 KBscott_euser
Command icon 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

scott_euser created an issue. See original summary.

scott_euser’s picture

Looks like this regression was introduced in #3426640: Cache metadata is not captured when no results

scott_euser’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new55.27 KB
joekers’s picture

Status: Needs review » Fixed

Thanks for raising and fixing the issue - merged into dev.

donquixote’s picture

Status: Fixed » Needs review

I 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.

donquixote’s picture

Status: Needs review » Fixed

Sorry for changing the status!

scott_euser’s picture

Yep 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.

scott_euser’s picture

Thanks for actioning this so quickly by the way, much appreciated! Was a bit worried about some higher traffic sites taking a performance hit.

donquixote’s picture

@scott_euser
Could we come up with a testing scenario to confirm that this works correctly on a given website?

scott_euser’s picture

Yeah 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.

Status: Fixed » Closed (fixed)

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