0

I have a Paragraph type called image - that holds a media reference and also a field which describes how the sibling image should be rendered. I look at the value of the "field_image_style" - and then apply a responsive image style to the Image in a preprocess_paragraph call:


function my_module_preprocess_paragraph(&$variables)
{

  if ($variables['paragraph']->hasField('field_image_style') && isset($variables['paragraph']->field_image_style->entity)) {
    /** @var \Drupal\Core\Entity\Entity\EntityViewMode $viewMode */
    $viewMode = $variables['paragraph']->field_image_style->entity->field_view_mode_for_media->entity;
    //kint($viewMode);
    if ($viewMode != null) {
      $viewModeMachineName = $viewMode->id();
      //kint($viewModeMachineName);
      $viewModeMachineNameMediaPrefixRemove = str_replace("media.", "", $viewModeMachineName);
      //kint($viewModeMachineNameMediaPrefixRemove);

      //kint($variables['content']['field_image'][0]['#view_mode']);
      $variables['content']['field_image'][0]['#view_mode'] = $viewModeMachineNameMediaPrefixRemove;
    }
  }
}

The html for rendering the image is being cached - and when I rebuild the cache - the site flips between one flavour and the next. How do I make the cache key on the value I am setting such that I get a cached copy for each different $viewModeMachineNameMediaPrefixRemove ?

Full data structure added below for completeness:

Paragraph type: Image Image contains two fields - both Entity references. field_image > Entity Reference > Media field_image_style > Entity Reference > Taxonomy Term (Image style)

The Taxonomy term Image style then has two fields; Tax term > Text plain (css format) - used to decorate the paragraph Tax term > Entity Reference > View mode (points to view mode of Media Image)

This view mode is then used to select primarily one of two Responsive Image Styles.

The preprocess hook is on the paragraph - as the paragraph knows about both the image and the way in which it needs to be rendered.

8
  • Where is your view mode field hosted, on the paragraph or on the media entity? $variables['paragraph']->field_image_style->entity->field_view_mode_for_media->entity looks a bit strange to me. If the view mode field is hosted by the media entity, better use its preprocess hook, instead of the paragraph hook.
    – Hudri
    Jul 1, 2022 at 16:23
  • Yeah, tbh, your description of the data structure doesn't match what your code shows for a data structure. In any event, I think you could resolve this by removing the cache keys from your field_image render array.
    – sonfd
    Jul 1, 2022 at 17:06
  • Thanks @Hudri - the view mode field is on a taxonomy reference field that is then attached to the paragraph wrapper. I've updated the question to detail more the structure. Jul 3, 2022 at 7:26
  • Thanks @sonfd - If a removed the cache key for the field_image - would that mean that HTML would never be cached? There will only ever be as many versions of the HTML as there are Image view modes - so wondered if I could fold that into the cache somehow. Jul 3, 2022 at 7:27
  • @Hudri - running with your thought of changing the hook - should I be setting the needed view mode in the paragraph hook into a temporary variable - then retrieving that in a preprocess hook for the image formatter perhaps? Saw this drupal.stackexchange.com/questions/245610/… Jul 3, 2022 at 7:36

1 Answer 1

0

Thanks to all for their contributions. Ended up using the solution proposed by @Hudri:

unset($variables['content']['field_image'][0]['#cache']['keys']);

Which allowed for both image styles to be used on the same image across different paragraphs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.