Clear media cache tag when changing a paragraph attachment setting

Created on 5 July 2021, over 3 years ago
Updated 7 March 2024, 10 months ago

Problem/Motivation

In drowl_paragraphs_types.module we had to add cache keys in this code to ensure each rendered media entity with different attachments display settings (added by our module) is cached on its own. Otherwise for example all media entities with ID 16 and tile are displayed like the first one rendered (with the first ones settings) because for cache they'd be all equal:

if($paragraph_type === "attachments"){
    if ($paragraph->hasField('field_paragraphs_item_color') && !$paragraph->get('field_paragraphs_item_color')->isEmpty()) {
      if (!empty($paragraph->get('field_paragraphs_item_color')[0]->getValue()['target_id'])){
        $color_taxonomy = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($paragraph->get('field_paragraphs_item_color')[0]->getValue()['target_id']);
        $button_color_class = $color_taxonomy->get('field_css_classes')->value;

        // We set the #drowl_media advanced twig variables:
        foreach($variables['content']['field_paragraphs_attachments']['#items'] as $key => $value){
          $variables['content']['field_paragraphs_attachments'][$key]['#drowl_media']['media_link_button_color'] = $button_color_class;
          $variables['content']['field_paragraphs_attachments'][$key]['#cache']['keys'][] = 'drowl_media:media_link_button_color:' . $button_color_class;
        }
      }
    }

    if ($paragraph->hasField('field_paragraphs_item_size') && !$paragraph->get('field_paragraphs_item_size')->isEmpty()) {
      $button_size_class = $paragraph->get('field_paragraphs_item_size')->value;

      // We set the #drowl_media advanced twig variables:
      foreach($variables['content']['field_paragraphs_attachments']['#items'] as $key => $value){
        $variables['content']['field_paragraphs_attachments'][$key]['#drowl_media']['media_link_button_size'] = $button_size_class;
        $variables['content']['field_paragraphs_attachments'][$key]['#cache']['keys'][] = 'drowl_media:media_link_button_size:' . $button_size_class;
      }
    }

    if ($paragraph->hasField('field_paragraphs_button_options') && !$paragraph->get('field_paragraphs_button_options')->isEmpty()) {
      $field_paragraphs_button_options = $paragraph->get('field_paragraphs_button_options')->getValue();
      if(!empty($field_paragraphs_button_options)){
        // We have to loop manually as each row has 'value' key... horrible structure:
        foreach($field_paragraphs_button_options as $key => $valueArray){
          if($valueArray['value'] == 'expanded'){
            // Expanded is selected:
            // We set the #drowl_media advanced twig variables:
            // We set the #drowl_media advanced twig variables:
            foreach($variables['content']['field_paragraphs_attachments']['#items'] as $key => $value){
              $variables['content']['field_paragraphs_attachments'][$key]['#drowl_media']['media_link_button_expanded'] = TRUE;
              $variables['content']['field_paragraphs_attachments'][$key]['#cache']['keys'][] = 'drowl_media:media_link_button_expanded';
            }
          }
          if($valueArray['value'] == 'hollow'){
            // Hollow is selected:
            // We set the #drowl_media advanced twig variables:
            foreach($variables['content']['field_paragraphs_attachments']['#items'] as $key => $value){
              $variables['content']['field_paragraphs_attachments'][$key]['#drowl_media']['media_link_button_hollow'] = TRUE;
              $variables['content']['field_paragraphs_attachments'][$key]['#cache']['keys'][] = 'drowl_media:media_link_button_hollow';
            }
          }
        }
      }
    }

Somehow when changing one of our custom settings in the paragraph, we'd also have to clear the referenced media cache tags to reflect the changes as cache might be warm with old settings.

Currently you may have to clear all caches to see the changes.

Typically this is handled by core for other cases but not for our custom fields set on the parent entity.

(Note: Drupal sets all the cache metadata in the above example automatically: the entity view builder and field formatters take care of that. It's just meant to be a concrete example that is intuitively understood by all.)

See: https://www.drupal.org/docs/8/api/render-api/cacheability-of-render-arrays

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇩🇪Germany Anybody Porta Westfalica

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.

Production build 0.71.5 2024