1

We have a text (formatted, long) field that is used to insert embed code such as the Youtube embed code for videos.

When printing the field in twig we are finding that rather than rendering the embed code, it is displaying the embed code as string. To get around this, we currently apply Twig's Raw filter, but wanted to see if there was a way to not use the raw filter and render the field as is.

Content type (basic page):

  1. body (text, formatted long) field
  2. title

Current results (inserted into Body field using Full HTML text format, displayed as string):

<script type="text/javascript" id="vidyard_embed_code_SkWfXrpFKFpS9JrytJwqMt" src="//play.vidyard.com/SkWfXrpFKFpS9JrytJwqMt.js?v=3.1.1&type=lightbox"></script><div class="outer_vidyard_wrapper"><div class="vidyard_wrapper" onclick="fn_vidyard_SkWfXrpFKFpS9JrytJwqMt();"><img width="360" src="//play.vidyard.com/SkWfXrpFKFpS9JrytJwqMt.jpg?" alt="Generating Leads with Video Content"><div class="vidyard_play_button"><a href="javascript:void(0);"></a></div></div></div>

Desired results (output of body field shows the video)

Video shown

  • 2
    Hmmm... seems like it would be safer to use one of the specialized youtube field modules / Media module to handle this. – Kevin Mar 6 '18 at 19:36
  • Just use the prepared formatter render array, aka content.body. See md-systems.ch/en/blog/techblog/2017/02/20/…. Agreed with comments that this is unsafe, anyone with access to that format can easily break your site, on purpose or accidently. – Berdir Mar 7 '18 at 14:22
0

You have to prepare rendered array, this is example for paragraph field.

<div class="accordion-body">
            {% set accordion_body = paragraph.field_text_of_accordion_entry.value %}
            {%  if accordion_body|length == 0 %}
              {% set accordion_body = ' ' %}
            {%  endif %}
              {% set body = {
                  '#type':   'processed_text',
                  '#text':    accordion_body,
                  '#format':  paragraph.field_text_of_accordion_entry.format,
                } %}
              <div class="accordion-body-container">
              {{body}}
              </div>
          </div>
  • thanks I didn't think to do that in the twig template. now it makes sense. – usernameabc Mar 7 '18 at 20:51
1

With the excellent Twig Tweak module you can do this:

{{ node.FIELD_NAME.value | check_markup(node.FIELD_NAME.format) }}

This feature (and more) is well documented on the module's cheat sheet.

  • This might work as well. {{ node.FIELD_NAME|view }} – ya.teck Jan 8 at 5:19

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.