I have a view where we are using the Body Summary field. I got a bug report that an HTML entity, &, displaying, where they wanted a literal ampersand.

When I updated our existing view, based on fields, to use the Body Summary field instead of a trimmed body, I did not find the Body Summary field by itself in the list of fields. So, I added another Body field to the view, and re-wrote its results: {{ body__summary }}.

Thinking that the problem was from our site's configuration, I repeated the same steps on a fresh Drupal install: using the Article content type, I added content in the Summary field, and created a view of fields of the Article content type. I got the same behavior on the fresh install.

I posted a question here, but I did not include information about what kind of view I created. I made a mistake from neglect; I did not include all the information needed to understand the situation and answer the question.

A helpful user went through the trouble of creating a fresh install, with two different versions of Core, and reported that he did not get the same behavior, but nonetheless gave a helpful and relevant answer.

That answer got me thinking, and I created a second view of teasers instead of fields. That view has literal ampersands, which is what I want. I concluded that leymanxx, when he did his testing, created a view of teasers, because I neglected to mention what kind of view I made, and thus, he did not see the same behavior that I did.

So now it looks to me that the body field re-write is the origin of the problem. I'm not sure the best way to address it:

  1. Did I overlook the summary field in the list of fields somehow? Is there a "right way" to add it, and I simply did it a wrong way?
  2. When I inspect the field with the escaped ampersand with theme debugging, it tells me this: <!-- THEME HOOK: 'views_view_field' --> <!-- BEGIN OUTPUT from 'core/modules/views/templates/views-view-field.html.twig' --> When I look at that theme file, apart from the comments, I see only this {{ output -}}. Should I override this template? I'm not sure I know how to fix only the HTML escaping issue while ensuring that I'm not creating unwanted side-effects for all my other views. It appears that all views with fields get processed by this template.
  3. Edit just make a new field, this is too much bother. See edits below.

How do I get a summary field displaying literal ampersand characters in a view of fields in Drupal 8?

Edit According to his comments, laymaxx didn't create a view of teasers, but added the body field and set the formatter to "summary or trimmed". We can't use this in our situation, because we never want the trimmed body; first, Drupal does a bad job trimming, and second, our wording in our summary will almost always be different from the first few lines of the body text. We want to use the summary and the summary alone.

At this point, I'm thinking just to add a new field and use that. However, I am curious as to the "right way" to do this, in case I am ever faced with a situation like this in the future.


1 Answer 1


Override the template and use the twig filter "raw":

  1. Add template file to your custom theme. E.g. /themes/custom/MYTHEME/templates/views-view-field.html.twig
  2. In the above template file add {{ output|raw }}
  3. Clear cache and refresh

The raw filter marks the value as being "safe", which means that in an environment with automatic escaping enabled this variable will not be escaped

Source: https://twig.symfony.com/doc/2.x/filters/raw.html

  • Does this have any potential side effects with other html in or strings entered in the field?
    – user1359
    Jul 2, 2019 at 12:31
  • 1
    It does yes. Essentially twig will not automatically escape any fields that it outputs. It's really up to you if you think that to be a security risk. If you want to bump up security, you could make the field override more specific. One way to this would be to use hook_suggestions_views_view_field_alter() to add a suggestion based on the field name & view name. Then you can override the output for that one specific view / field.
    – 100pic
    Jul 2, 2019 at 23:49

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.