Score:0

What cache tags can be triggered by a change in state variables?

ca flag

If I want my render array to indicate to Drupal that it should be rebuilt when, say, node 1 changes, I can do something something like this:

$my_render_array = [
  ...
  '#cache' => [
    'tags' => [
      'node:1',
    ],
  ],
]; 

How can I tell Drupal that my render array should be rebuilt when the state variable foo changes?

I tried this but it does not work:

$my_render_array = [
  ...
  '#cache' => [
    'tags' => [
      'state:foo',
    ],
  ],
];

Running:

drush sset foo bar

Does not trigger an invalidation of my cache.

Right now this is a two-step process for me, I need to do something like this:

$my_render_array = [
  ...
  '#cache' => [
    'tags' => [
      'some_custom_tag_that_i_invented',
    ],
  ],
];

drush sset foo bar
drush php "\Drupal::service('cache_tags.invalidator')->invalidateTags(['some_custom_tag_that_i_invented'])"

Drupal core seems to use cache tags in the form "state:...", although not much and only within automated tests:

# grep -R 'invalidateTags' core|grep state
core/modules/jsonapi/tests/src/Functional/CommentTest.php:    Cache::invalidateTags(['state:jsonapi__entity_test_filter_access_blacklist']);

# grep -R 'state:jsonapi__entity_test_filter_access_blacklist' core
core/modules/jsonapi/tests/src/Functional/EntityTestTest.php:      $cacheability->addCacheTags(['state:jsonapi__entity_test_filter_access_blacklist']);
core/modules/jsonapi/tests/src/Functional/CommentTest.php:    Cache::invalidateTags(['state:jsonapi__entity_test_filter_access_blacklist']);
core/modules/jsonapi/tests/src/Functional/CommentTest.php:      $cacheability->addCacheTags(['state:jsonapi__entity_test_filter_access_blacklist']);
core/modules/jsonapi/src/Access/TemporaryQueryGuard.php:        $cacheability->addCacheTags(['state:jsonapi__entity_test_filter_access_blacklist']);

# grep -R 'jsonapi__entity_test_filter_access_blacklist' core
core/modules/jsonapi/tests/src/Functional/EntityTestTest.php:      $cacheability->addCacheTags(['state:jsonapi__entity_test_filter_access_blacklist']);
core/modules/jsonapi/tests/src/Functional/CommentTest.php:    \Drupal::state()->set('jsonapi__entity_test_filter_access_blacklist', [$this->entity->getCommentedEntityId()]);
core/modules/jsonapi/tests/src/Functional/CommentTest.php:    Cache::invalidateTags(['state:jsonapi__entity_test_filter_access_blacklist']);
core/modules/jsonapi/tests/src/Functional/CommentTest.php:      $cacheability->addCacheTags(['state:jsonapi__entity_test_filter_access_blacklist']);
core/modules/jsonapi/src/Access/TemporaryQueryGuard.php:        $blacklist = \Drupal::state()->get('jsonapi__entity_test_filter_access_blacklist', []);
core/modules/jsonapi/src/Access/TemporaryQueryGuard.php:        $cacheability->addCacheTags(['state:jsonapi__entity_test_filter_access_blacklist']);
Jaypan avatar
de flag
I don't see that Drupal core provides any cache tags for state, so I think you'd have to use your own custom tag.
ca flag
Thanks. That wouldn't be a bad feature. I opened a feature request at https://www.drupal.org/project/drupal/issues/3258064
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.