After setting a cache tag to use a contextual filter in a view, when checking the cache tags returned on page load both the processed and unprocessed tags are included in the cache tags header.
For example. If a view has a contextual filter of taxonomy term id (tid) and custom cache tag 'custom:{{ arguments.tid }}. When the view is loaded with the term id value of 5, then the X-Drupal-Cache-Tags header will have two values added, one called 'custom:5' and the other 'custom:{{ arugments.tid }}'.
Similar to what's reported in the last comment of this issue: https://www.drupal.org/project/views_custom_cache_tag/issues/2971568
I've raised this as a bug as according to the Cache Tags documentation here: https://www.drupal.org/docs/8/api/cache-api/cache-tags the tags cannot contain spaces (which views replacement tokens all contain). If this issue is not a bug but instead the desired behaviour then feel free to close it.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | views_custom_cache_tag-exclude-token-3115736.patch | 808 bytes | stooit |
Issue fork views_custom_cache_tag-3115736
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
stooit commentedI can replicate this on a fresh install:
custom:{{ raw_arguments.nid }} custom:10This simple patch will ensure no raw token values are returned.
Comment #3
pcate commentedI just installed this module for testing and noticed this as well. For me it just caused confusion as I thought the tokens weren't being processes as cache tags, and I thought I was doing something wrong.
The patch applied for me, but unfortunately caused the following error to be thrown on pages with the view:
Comment #4
pcate commentedComment #5
pcate commentedComment #6
pcate commented@stooit I'm wondering if the error I am getting is related to using
array_mapto filter out the unprocessed tags. Specifically, if thestrposcheck you added is true, an empty array item is probably being added to$custom_tags.array_filtermight be a better function to use.Comment #7
berdirYou can't change the value with array_filter() so you'd need both.
I don't understand why they would get duplicated though. Sounds to me like you might have another view that doesn't have the same argument or not value for it?
The only cases where I've seen those kind of cache tags on all the sites where we use this for is due to misconfiguration.
The problem with this approach is that it might fix the immediate issue of having invalid cache tags. But if we just drop those cache tags, then the views will not be invalidated when they should. Maybe this should be combined with a log message that logs which view/display this happened?
Comment #8
sonvir249 commented@Berdir I see this issue for views blocks but not for views pages.
Comment #9
vivek panicker commentedI can replicate this issue in D10.3 also in a view block.
Comment #11
vivek panicker commentedUpdated the MR to make it compatible with the latest code version.
Comment #12
vivek panicker commentedComment #13
mdolnik commented@Berdir
I am having the same issue as everyone else when embedding a view on a page.
How to reproduce:
my-custom-tag:{{ arguments.field_name }}$render_array = Views::getView($view_name)->buildRenderable($display_name, [$arg]);$render_array['#cache']['tags']and notice that the tokens remain un-resolvedAfter investigation, the following is occurring:
buildRenderable()will:DisplayPluginBase::buildRenderable()DisplayPluginBase::applyDisplayCacheabilityMetadata()ViewExecutable::getCacheTags()CustomTag::getCacheTags()DisplayPluginBase::applyDisplayCacheabilityMetadata()at this stage so there are no values to process the tokens with.View::preRenderViewElement()is called during render.ViewExecutable::executeDisplay()Block::execute()ViewExecutable::render()CachePluginBase::cacheSet()CustomTag::getCacheTags()ViewExecutable::render()will then:DisplayPluginBase::render().DisplayPluginBase::applyDisplayCacheabilityMetadata()ViewExecutable::getCacheTags()CustomTag::getCacheTags()applyDisplayCacheabilityMetadata()is applying the cache tags to an existing element, and this element already contains the cache tags with the un-resolved tokens from the first call toCustomTag::getCacheTags(), so the element ends up getting both resolved and unresolved tags.As for blocks, the process is the same, where
ViewsBlock::build()will callDisplayPluginBase::buildRenderable().I was playing around with modifying
CustomTag::getCacheTags()in an attempt to extract the arguments from the element/render array before callingtokenizeValue():...Which seemed to work for embedded views where the arguments are passed into
buildRenderable(), but I did not have luck with embedded blocks as it doesn't seem to have the arguments by the time it callsbuildRenderable().With my specific issues, the other proposed code in this issue queue seems to resolve the invalid cache tags, while retaining proper caching.
Since the issue is only in the initial render array building and the views render and views results caching end up providing the proper cache tags, changing
CustomTag::getCacheTags()to omit the initial broken cache tags seems to have no ill effects.