3

I have a block that is cached per page. However, I need a way to invalidate it's cache in code per page or node. (Or, if no way, all cache per block id).

4

The SystemMenuBlock of core is invalidated when its menu changes because this method:

public function getCacheTags() {
  // Even when the menu block renders to the empty string for a user, we want
  // the cache tag for this menu to be set: whenever the menu is changed, this
  // menu block must also be re-rendered for that user, because maybe a menu
  // link that is accessible for that user has been added.
  $cache_tags = parent::getCacheTags();
  $cache_tags[] = 'config:system.menu.' . $this->getDerivativeId();
  return $cache_tags;
}

I think you can solve your problem in the same way.

  • Thank you! Your answer has pointed me in the right direction. – Alexei Rayu Jun 22 '16 at 10:46
  • @AlexeiRayu did you get this working? I have a similar issue with a block that extends SystemMenuBlock. All the node tags are added correctly but I can't seem to get it to invalidate on a node save. – masterchief Nov 28 '16 at 21:41
  • @masterchief I got this working, yes. I ended up checking the way that Drupal 8 caching works - it actually has tags that cover all those requests. I don't remember what precisely I did - but I remember it was some tag, something standard. – Alexei Rayu Nov 30 '16 at 9:18
  • @AlexeiRayu nice! I had mine solved in a slightly different way. The problem was with our template, we were prepocessing the menu and creating a new key that wasn't set as the render element in the theme hook. This meant that the render element that contained the cache tags were being ignored! – masterchief Dec 1 '16 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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