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']);