Preprocess image: how to get parent entity? I'm using responsive images (single img tag with srcset and sizes attributes). My content editors can manipulate the displayed size of the image with a custom field on a shared parent entity. Therefore I can't meaningfully use the sizes field from the responsive image module, because my editors can set the image size from 10% of a fixed container up to 100vw on per paragraph level.

I know how to preprocess the sizes attributes, but I don't know how to get to my shared parent entity, which holds the field determining the size of the image's parent container:

function template_preprocess_responsive_image(&amp;$variables) {
  // pseudocode: don't know how to this
  $imageField = $variables['whatever'];
  $parentParagraph = $imageField-&gt;getParentMediaEntity()-&gt;getParentParagraphEntity();
}


My question is how do I get the parent paragraph entity as written above in pseudocode?
Here is the the "real" code counterpart to the pseudecode from the question:

function THEMENAME_preprocess_responsive_image_formatter(array &amp;$variables) {
  // get the imagefield's parent entity
  $fieldParent = $variables['item']-&gt;getEntity();

  // this line is only needed if using media entities (= entity references)
  // must disable entity view caching to work correctly
  $fieldParent = $fieldParent-&gt;_referringItem-&gt;getEntity();

  // get the image width from the custom field
  $myImgSizes = $fieldParent-&gt;get('field_image_size')-&gt;getString();
  // can't change attribute here =&gt; store it temporarily
  $variables['responsive_image']['#attributes']['data-overridesizes'] = $myImgSizes;
}

function THEMENAME_preprocess_responsive_image(array &amp;$variables) {
  //override sizes attribute with my calculated value
  if (key_exists('data-overridesizes', $variables['img_element']['#attributes'])) {
    $variables['img_element']['#attributes']['sizes'] = $variables['img_element']['#attributes']['data-overridesizes'];
    unset($variables['img_element']['#attributes']['data-overridesizes']);
  }
}


If using media (=entity references) you also have to disable view cache for entites (see this question)

function MODULENAME_entity_view(&amp;$build, $entity, $display, $view_mode) {
  if ($entity-&gt;getEntityTypeId() == 'media' &amp;&amp; $entity-&gt;bundle() == 'image' &amp;&amp; $view_mode == 'my_uncached_viewmode'){
    unset($build['#cache']['keys']);
  }
}