Use a different twig media template if it is being rendered inside a WYIWYG

cn flag

I have a twig template for a media entity that I wish to be able to embed into a WYIWYG. I am able to, however the entity has issues rendering when inside the ckeditor that aren't present when rendered on an actual page. I want to either use a different template or use some variable that detects whether the media is in an edit mode.

Is there a twig variable like {{ inEditor }} or is there a template I could use specific to wyiwygs like media--remote-video--WYIWYG.html.twig ?

When viewing the element in a preprocess function I do see there is a 'view_mode' variable but that's always set to default.

Kevin avatar
in flag
May be easier to allow someone to select the view mode of the media being inserted, then you can provide a twig file for each one.
cn flag

You could check for the route name media.filter.preview:

if (\Drupal::routeMatch()->getRouteName() === 'media.filter.preview') {
  // media is rendered inside CKEditor
  $variables['inEditor'] = TRUE;
// add cache context
$variables['#cache']['contexts'][] = '';
Matt avatar
cn flag
Which preprocess hook could I use this in?
4uk4 avatar
cn flag

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.