Problem/Motivation
IMHO it is a good practice to not hardcode cache tags in custom code and instead generate them programmatically.
For example, when writing a render array that varies when a node is added or removed, it is possible to use EntityType::getListCacheTags (and EntityType::getListCacheContexts) to generate the appropriate node_list tag.
Since #2145751: Introduce ENTITY_TYPE_list:BUNDLE cache tag and add it to single bundle listing we have a ENTITY_TYPE_list:BUNDLE cache tag but no way to generate it programmatically.
Steps to reproduce
$cache = new CacheableMetadata();
// I can add node_list programmatically:
$cache->addCacheTags(\Drupal::entityTypeManager()->getDefinition('node')->getListCacheTags());
$cache->addCacheContexts(\Drupal::entityTypeManager()->getDefinition('node')->getListCacheContexts());
// But if I only care about a single bundle, I have to know the tag name:
$cache->addCacheTags(['node_list:' . $bundle]);
Proposed resolution
Maybe something like this:
\Drupal::entityTypeManager()->getDefinition('node')->getBundleListCacheTags('mybundle');
Remaining tasks
User interface changes
Introduced terminology
API changes
Developers can use the new EntityType::getBundleListCacheTags() method to generate the appropriate cache tags if we want a cached element to be invalidated when a node of a specific bundle is updated, deleted or added.
Data model changes
Release notes snippet
Issue fork drupal-3501508
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3501508-add-a-way
changes, plain diff MR !11400
Comments
Comment #2
quietone commentedComment #3
catchThis seems like a good idea to me.
Comment #5
prudloff commentedComment #6
smustgrave commentedSeems like a good idea
Seems like API change so can that section be updated and probably need a change record as well.
If you are another contributor eager to jump in, please allow the previous poster at least 48 hours to respond to feedback first, so they have the opportunity to finish what they started!
Comment #7
prudloff commentedComment #8
prudloff commentedI drafted a change record but I don't have any experience writing those.
Comment #9
smustgrave commentedCR seems fine.
I tweaked it slightly with a before snippet to show how this use to be done and now what's possible.
Comment #11
catchCommitted/pushed to 11.x, thanks!
Comment #13
smustgrave commentedPublished CR