2

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.

1
  • 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 at 18:04

1 Answer 1

3

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';
2
  • Which preprocess hook could I use this in?
    – Matt
    May 16 at 20:47
  • 1
    hook_preprocess_media()
    – 4uk4
    May 16 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.