- πΊπΈUnited States sea2709 Texas
I run into this issue where I have a view listing all locations based on a proximity search returned from Solr. The issue happens when there is a distance info in the location row to indicate how far from the location filter to the current location in each row. When a user do a search, the search query will be presented in the URL. So in my case, views row cache should have the cache context 'url.query_args'.
This is the code from StylePluginBase.php$data = [ '#pre_render' => [[$this, 'elementPreRenderRow']], '#row' => $row, '#cache' => [ 'keys' => $cache_plugin->getRowCacheKeys($row), 'tags' => $cache_plugin->getRowCacheTags($row), 'max-age' => $max_age, ], '#cache_properties' => $field_ids, ]; $renderer->addCacheableDependency($data, $this->view->storage);
The patch on #7 π Views row cache do not respects display contexts Active suggested adding the cache context from the display to the rows cache. I think it's a good approach, but I think I will create so many cache renderer records based on the some common contexts from the display like "languages", "url", "url.query_args", etc. for each individual row in a view.
Because of that, I think adding more cache context for views cache row should be specific for views. Most of the cases, we don't need adding these contexts.
I noticed that the we add cache dependency for
$data
based on the$this->view->storage
. So I came up with a solution to implement the hook views_pre_render, in this hook I will add appropriate cache contexts that needed for the rows cache. In details of my case, it looks like this:function mymodule_views_pre_render(ViewExecutable $view) { if ($view->id() === 'someview' && $view->current_display === 'somedisplay') { // View row cache doesn't have the cache context query_args, since query_args contains info // about the proximity search, and different proximity query search has different distance info $view->storage->addCacheContexts(['url.query_args']); } }
- π³πΏNew Zealand quietone
@sea2709, on what version of Drupal are you seeing this problem? This is filed on 8.9.x, which is EOL.
- πΊπΈUnited States sea2709 Texas
@quietone : I'm using Drupal 10.3.1
- π³πΏNew Zealand quietone
@sea2709, thanks. Then the version here needs to change to 11.x. That is our 'main' development branch and any change will be backported according to our policies.
This also needs an issue summary update, I have added the standard template to help with that.