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.

  • 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.
    – Kevin
    May 16, 2022 at 18:04

1 Answer 1


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'][] = 'route.name';
  • Which preprocess hook could I use this in?
    – Amy
    May 16, 2022 at 20:47
  • 1
    – 4uk4
    May 16, 2022 at 21:05

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.