Score:0

Programmatically change view mode of media field

cn flag

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?

sonfd avatar
in flag
Have you tried [hook_entity_view_mode_alter()](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/function/hook_entity_view_mode_alter/9.3.x)?
Matt avatar
cn flag
@sonfd I have but that hook doesn't get triggered by my media field.
4uk4 avatar
cn flag
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 avatar
cn flag
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).
Matt avatar
cn flag
@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.
4uk4 avatar
cn flag
Yes, you can add fields to a media entity and the field in the code example is also such a field, What is confusing in the question is what you mean by media field, a field like this you can access in the hook of the code example or a field in a different entity which needs a different hook?
Matt avatar
cn flag
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.
Score:2
cn flag

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'.

sonfd avatar
in flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.