How to use hook_field_preprocess to add a prefix/suffix to an entity reference field for different view modes?
If you have configured the reference field to render the referenced entities then the build array usually contains #cache keys, which result in a separate cache entry for this render array including anything you've attached to it in preprocess. This has nothing to do with the view mode of the parent entity, the cached result will also be reused when the media is displayed in a different entity.
You could split up the field content in multiple render elements so that you have the prefix/suffix outside of the cached element content:
$variables['items'][0]['content'] = [
  'prefix' =&gt; ['#markup' =&gt; $prefix],
  'content' =&gt; $variables['items'][0]['content'],
  'suffix' =&gt; ['#markup' =&gt; $suffix],
];