1

I'm creating a view that displays a few fields from a specific content type. Among the fields is the trimmed nody body. The problem is that the HTML tags in the body are escaped and are visible as source in the view.

So instead of

Lorem ipsum dolor sit amet, in dolores propriae sapientem vim. Per no autem minimum iudicabit, ad numquam eloquentiam ius. Quas iusto ea vis. Mea quaestio gloriatur mediocritatem et. Mei rebum postea ne, his malorum necessitatibus ei. Ea pro animal mandamus, ad pri postea eripuit.

I get the following:

<p>Lorem ipsum dolor sit amet, in dolores propriae sapientem vim. Per no autem minimum iudicabit, ad numquam eloquentiam ius. Quas iusto ea vis. Mea quaestio gloriatur mediocritatem et. Mei rebum postea ne, his malorum necessitatibus ei. Ea pro animal mandamus, ad pri postea eripuit.</p>

From what I understand Drupal 8 escapes everything by default, so I suspect this has something to do with my problem. I looked at every setting I could find in Views, but I couldn't find anything that would change the escaping of HTML tags.

How can I output the body of a node unescaped in a Drupal 8 view?

3
  • I believe I saw an issue on drupal.org dealing with exactly this bug. Maybe it is already resolved and you just need to use the newest beta. Else you should search for the issue and apply the proposed patch manually in case it is urgent.
    – alexej_d
    Commented Aug 30, 2015 at 15:52
  • Yes, it may be this: drupal.org/node/2363423, committed to 8.0.x but it was after beta 14, so if you're just following betas it will be in beta 15.
    – star-szr
    Commented Sep 3, 2015 at 15:58
  • Just to be 100% sure, the field is a full_html text type, right?
    – Andreas
    Commented Sep 3, 2021 at 7:18

1 Answer 1

0

I'd recommend against accepting this right away even if it works for you because I suspect it isn't the best answer, but I ran into this same problem and I ended up just overriding the template with raw HTML like so:

  1. Copy core/modules/views/templates/views-view-fields.html.twig to the appropriate location in your own theme (e.g. themes/custom/mytheme/templates/views/views-view-fields.html.twig for me)
  2. For each variable output in the loop add the raw filter, e.g. {{ field.content | raw }}
  3. Clear your caches and refresh the page to see the result

I think this should be okay since any HTML that comes from the views fields should already be filtered of anything harmful (and I believe this is more or less how it used to be done - take a look at the old PHP views templates) but if someone knows better please do correct me in the comments.

EDIT: I noticed one of the related questions (Drupal 8: HTML isn't rendering, HTML tags are being printed) has a link to this issue: https://www.drupal.org/node/2363423 which I believe is related, so this workaround may soon no longer be necessary.

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.