Views row cache do not respects display contexts

Created on 11 February 2016, almost 9 years ago
Updated 18 October 2024, about 2 months ago

Code committed in https://www.drupal.org/node/2450897 β†’ not adding 'views display' cache context to row cache data.
It only adds 'view' cache context. This produces strange behaviour when you try to preprocess a views field (hook_preprocess_views_view_field()).

If you have exposed filter in the view and do some logic in your preprocess hook based on exposed filter value - this hook will be executed only once per row field, for first value of exposed filter. All other requests will return cached version when you change exposed filter value. You might not reproduce this bug because one of 'row' cache keys is a hash of row data. So it reproduces only when the row is the same but exposed filter value is different.

πŸ› Bug report
Status

Active

Version

8.9 ⚰️

Component

views.module

Created by

πŸ‡³πŸ‡±Netherlands undertext

Live updates comments and jobs are added and updated live.
  • views

    Involves, uses, or integrates with views. In Drupal 8 core, use the β€œVDC” tag instead.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡Έ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.

Production build 0.71.5 2024