I'm attempting to change the view mode of a media field programmatically. I've created a view mode called 'modal'. I created a mytheme_preprocess_media hook and attempted to change the view mode a variety ways like

mytheme_preprocess_media(&$variables) {
    $variables['view_mode'] = 'modal';
    $variables['elements']['#view_mode'] = 'modal';
    $variables['content']['field_media_oembed_video']['#view_mode'] = 'modal';

but none seem to work. Using xdebug I know for sure that this hook is being hit but for some reason it always uses the default view mode.

How can I programmatically change the view mode of a media field?

  • Have you tried hook_entity_view_mode_alter()?
    – sonfd
    Commented May 10, 2022 at 0:26
  • @sonfd I have but that hook doesn't get triggered by my media field.
    – Amy
    Commented May 10, 2022 at 0:54
  • Fields don't have view modes, rendered entities have. The values shown in the question are only as info, you can't change them. If, however, the field is rendering referenced entities, you can try to change the view mode of those, if you find them in the field deltas. But this depends on how the field is configured and you also need to change the cache keys then as well.
    – 4uk4
    Commented May 10, 2022 at 7:48
  • 2
    OK, just checked the field name and found out it's a core preinstalled field and the field type is not a reference field. I think you are totally lost and need to start over one level up in the node (if the media is a in a content type).
    – 4uk4
    Commented May 10, 2022 at 9:59
  • @4uk4 what confuses me is if the field doesn't support view modes, why is it letting me add view modes in the admin interface? Also isn't media an entity? It's something I can add fields to.
    – Amy
    Commented May 10, 2022 at 12:03

1 Answer 1


In my wysiwyg I'm embedding media entities. When I embed a media entity I want to be able to change the view mode that is used.

When you embed a media entity in WYSIWYG you can select a view mode. If you want to change this later programmatically you have to change in the <drupal-media> tag the attribute data-view-mode. If you didn't specify a view mode this attribute is missing and you have to add it.

If you want to use a theme preprocess hook then use that of the template containing the WYSIWYG field, not that of the media template. Look for the WYSIWYG field in $variables['content'] and change the HTML source you find in '#text'.

  • 1
    In text format settings, e.g. /admin/config/content/formats/manage/full_html (Configuration > Content Authoring > Text formats and editors > [Format]) you should see a tab near the bottom titled "Embed media" where you can configure which media bundles can be embedded, which view modes are allowed, and which view mode is the default.
    – sonfd
    Commented May 10, 2022 at 13:38

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.