Disable caching for custom block for anonymous users

by Thomas0878   Last Updated January 11, 2018 10:07 AM

I have created a custom Block that extends BlockBase. I don't want the content to be cached for anonymous users (also not for authenticated users). To be more precise, caching would be fine but depending on some url query argument. In my case the url contains a parameter 'mode' (?mode=1 or ?mode=2 and so on). Depending on the parameter the content of the block varies.

I added the following function:

 public function getCacheContexts() {
    return Cache::mergeContexts(parent::getCacheContexts(), array('url.query_args:mode'));

Still the block content seems to be cached. Is it neccessary to disable the internal page cache module? Actually I don't want to globally disable caching, I just want my block to be cached correctly depending on the url parameter mode.

Tags : 8 blocks caching

Related Questions

How to correctly setup caching for my custom block?

Updated April 28, 2016 08:03 AM

Custom Block Class with custom Template, cache issue

Updated January 18, 2018 21:07 PM

Prevent block from caching

Updated June 25, 2016 08:03 AM