I am building a module with a block that outputs some informations from the current node.

When i return my render array i managed to get the cache as "node dependent" using (maybe not the best method ?) :

        '#cache' => [
            'contexts' => [

The blocks are showing up the right contents on the right pages, but if i modify my node and it's fields, the block content remains pulled from the cache and is not updated with the latest values.

Is there anything to add in the #cache array or a way to invalidate this specific cache when node gets updated ?

2 Answers 2


See my answer to a similar question here: Passing current node ID to the block template

In short, use the plugin context system to get the current node injected. It will automatically ensure that the right cache tags and contexts are added, based on where node is coming from.

Depending on what information you need exactly, you could also consider using the default entity view blocks provided by ctools. That allows you to view the node in a given view mode.


Allright, I found an answer using cache tags and the current node id like this :

'tags' => array('node:' . $node->id()),

The full cache array being :

'#cache' => array(
                'tags' => array('node:' . $node->id()),
                'contexts' => array('url'),

Ref : https://www.drupal.org/developing/api/8/cache/tags

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.