Caching issue with Brick content and CSS classes

Created on 28 January 2022, over 2 years ago
Updated 3 March 2023, over 1 year ago

Problem/Motivation

Using Bricks and ECK Entities cache tags aren't cleared on node save when brick entity content or css classes are changed. This appears to be an issue related to Drupal 9 changes as it was first noticed post Drupal 9 upgrade for our client. I've confirmed this behaviour with a fresh install of Drupal 9.3 and the latest stable ECK, Bricks and Inline Entity Form modules.

Steps to reproduce

  • Create a content type using a bricks fields that references ECK Entity bricks
  • Form display uses Bricks tree (inline entity form)
  • Create a node with some bricks, and save
  • Edit the node and change the content of one or more bricks
  • Save the node
  • Changes don't show until caches are clear
  • Edit again, this time changing only the CSS classes on one or more bricks
  • New or changed classes do not appear until caches are cleared.

Proposed resolution

Within my own project I've added a hook_entity_presave to invalidate the brick:entity_id cache tag on any save of my ECK type that has allowed changes to the content of the Brick entity. Changes to the CSS class still don't cause a cache invalidate/clear on the entity with this approach however. It could be an issue with the field widget or formatter.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada ibullock London, ON

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States jennypanighetti

    I'm running into this same issue. Any advice on how you wrote your hook to clear it??

    -Jenny

  • πŸ‡¨πŸ‡¦Canada ibullock London, ON

    Hi Jenny - Here's a look at what we ended up implementing. It actually changed a little since I created this issue so that we could include nested bricks in the cache invalidation. You'll need to change the field machine name at the very least, but hopefully it helps!

    /**
     * Implements hook_entity_presave().
     */
    function mymodule_components_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
      if ($entity->getEntityType()->id() == 'node' && $entity->field_components !== NULL) {
        $bricks = $entity->field_components->referencedEntities();
        foreach ($bricks as $brick) {
          Cache::invalidateTags(array('brick:' . $brick->id()));
        }
      }
    }
    
Production build 0.69.0 2024