Media and cache keys I'm using images inside of a Paragraph, and trying to use checkboxes in the Paragraph to control whether to display the credit and/or caption of the images. 

The problem is that the image gets cached the first time it loads and subsequent instances ignore the checkbox values.

I'm trying to set that cache keys so that each Paragraph + image gets its own cache instance, but keep getting errors saying that I'm not allowed to set cache keys.

I've tried in hook_media_preprocess, hook_paragraph_preprocess, hook_entity_view, hook_entity_view_alter, but no luck. 

Could someone point me in the right direction?

Thanks,

Luca
This might sound like a dumb solution, but the simplest and fastest solution of hiding fields would be hiding with CSS depending on a parent (paragraph) attribute or class. E.g. in paragraph preprocess

if ($paragraph-&gt;get('your_field')-&gt;value == "hide credit") {
  $variables['attributes']['class'][] = 'paragraph--credit-hidden';
}


and in CSS

.paragraph--credit-hidden .field--credit {
  display: none;
}


Other options would be either additional view modes for media entities (if not to many permutations), additional cache contexts (depending on the case, given your problem description most likely not a good solution), corresponding to your host paragraph settings. But given your description I'd go the KISS way and avoid the coding overhead.