I have a custom block building a menu. The cache works fine until a menu item is enabled/disabled in the menu edit, e.g.

admin/structure/menu/manage/main toggle the enabled checkbox and save.

The block does not reflect changes until the cache is cleared.

I have:

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

which picks up the url / menu text changes and also makes sure the block is valid for each node it is on [i.e. not showing the same block content on every node, but this node's version].

What tag do I need to make sure the cache is invalidated when menu item:enabled is toggled?

| improve this question | | | | |

You have to add the cache tag of the menu, in this case the main menu:

  '#cache' => [
    'tags' => [
| improve this answer | | | | |
  • Hi @4k4, thank you for the answer. I had tried a few variations of that tag, for example, the last attempt I had was: config:system:menu.main - is there somewhere I can get the patterns for common tags? I looked at drupal.org/docs/8/api/cache-api/cache-tags and saw 'thing:identifier', but did not make the leap to the correct tag format. – ice70 Dec 18 '18 at 11:00
  • Enable debugging of cacheable reponses and check the response headers in the browser F12 tools. You could for example place a core menu block on the page and see the cache tags added by this block. – 4k4 Dec 18 '18 at 11:12

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.