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

Command icon 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:

Comments

prudloff created an issue. See original summary.

quietone’s picture

Version: 11.1.x-dev » 11.x-dev
catch’s picture

This seems like a good idea to me.

prudloff’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs change record

Seems 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!

prudloff’s picture

Issue summary: View changes
prudloff’s picture

Status: Needs work » Needs review

I drafted a change record but I don't have any experience writing those.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs change record

CR seems fine.

I tweaked it slightly with a before snippet to show how this use to be done and now what's possible.

  • catch committed e8fa2b37 on 11.x
    Issue #3501508 by prudloff, smustgrave: Add a way to programmatically...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x, thanks!

smustgrave’s picture

Published CR

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.