0

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']);
2
  • 3
    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.
    – Jaypan
    Jan 11 at 17:44
  • Thanks. That wouldn't be a bad feature. I opened a feature request at drupal.org/project/drupal/issues/3258064
    – alberto56
    Jan 11 at 18:11

Your Answer

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

Browse other questions tagged or ask your own question.