Drupal

How to Fix Caching for Views With Exposed Filters in Drupal 7

One of the better features of the views module in Drupal is the ability to cache your view's output. This can come in handy when your view is doing a lot of computation.


Filed under:

One of the better features of the views module in Drupal is the ability to cache your view's output. This can come in handy when your view is doing a lot of computation. Caching your view will save your server a lot of unneeded work. One of the big current drawbacks of this feature is if you enable caching for your view and you have an exposed filter, you'll run into the following scenario:

User A enters a value for the filter


Then User B enters a different value for that same filter


Yikes! As you can see, caching is serving up User A's result set for User B! User B just wanted to see results in North Carolina and recieved results in Oregon! This is not very helpful as-is. Thankfully, there is a handy patch to views available to you at: http://drupal.org/node/1055616. Let me go ahead and explain how to apply this patch so you can have nice cached views with exposed filters in Drupal 7.

UPDATE: Since I wrote this article, there has since been another patch released. Feel free to try and use later patches (#81 and higher) instead of #71. If you do this, be sure to fully test views' caching to the fullest you can before deploying this fix anywhere.

  • Fire up a terminal and navigate to your drupal directory: cd ~/Sites/my_drupal_site/drupal
  • Update the views module: with drush: drush dl views or use http://yoursite.tld/admin/modules/update )
  • Change to the views module directory: either cd sites/all/modules/views or cd sites/all/modules/contrib/views
  • Download the patch (I'm using #71 as it worked best for me at the time of this writing). wget http://drupal.org/files/views-1055616-71.patch
  • Apply the patch: patch -p1 < views-1055616-71.patch
  • Clear your caches

You should now be able to test your cached view and see different results for different values entered into your exposed filters.

User A enters a value for the filter


and then User B enters a different value for that same filter


Much better, right? Hopefully this patch will become part of the next dev release of views, and make its way into a stable release, so this functionality will work out of the box. Happy Drupaling!

Similar posts

Get notified on new marketing insights

Be the first to know about new B2B SaaS Marketing insights to build or refine your marketing function with the tools and knowledge of today’s industry.