Search code examples
drupaltwigparagraph

How to render html iFrame from Drupal paragraph using plain text field


I have a site where I am trying to allow the site owners to enter an iframe embed code (using the paragraphs module) on a page and then will render this out adding a container to make it always responsive.

I am running into an issue with twig always outputting as text however. It works perfectly if I use a formatted text field and then click "source" and paste the code in from the dashboard, but obviously that is super clunky for the site admin and very prone to errors. I'd like to just have a plain text field instead. Trouble is even when I use {{ content.field_embed[0].value | raw }} it comes out as text. is there anyway around this that I am missing? Would like to not resort to having the user work with the CK editor just for a simple embed


Solution

  • @mhfleming, I was having the exact same issue as you. Using the filter raw was still giving me the escaped iframe, like in <iframe .... I even thought the issue could be related to one of the contrib modules Twig Tweak and Twig Field Value, which the site I'm working on uses, but those were not the culprits.

    I was able to get my code working with the following Twig snippet:

    {% set embed_code = {
      '#type': 'processed_text',
      '#text': content.field_embed_code[0]['#context']['value'],
      '#format': 'full_html',
    } %}
    <div class="responsive-embed">
      {{ embed_code }}
    </div>
    

    Your code looks very similar. Try replacing the field name with field_embed and give this snippet a try.