By Pim


2017-02-12 22:45:58 8 Comments

myfield is a plain text field. {{ content.field_myfield }} renders the field with HTML markup and the field label.

How do I render the raw value of the field? {{ content.field_myfield.value }} doesn't work.

3 comments

@Wasim Khan 2019-03-07 09:07:34

{{ content.body | raw }} 

get the raw field value in a twig template

@No Sssweat 2017-02-12 23:23:27

{{ entity.field_name.value }} to get the true raw value, includes tags and encoding.

  • Example: {{ node.body.value }}
  • result: <p>Batman &amp; Robin</p>

{{ content.field_name.0 }} to get the raw value minus tags and encoding.

  • Example: {{ content.body.0 }}
  • result: Batman & Robin

raw

This filter should be avoided whenever possible, particularly if you're outputting data that could be user-entered. See this page for more information on auto-escape in Drupal 8.

source: Filters - Modifying Variables In Twig Templates

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 if raw is the last filter applied to it

source: Twig's official docs


For example, you can use:

{{ node.body.value|striptags }}
{{ paragraph.field_text.value|striptags }}

The problem with using twig's |striptags is double encoding of html entities, not markup, so & becomes & and then &amp; – Berdir

@Berdir 2017-02-13 07:21:00

the content examples are not correct. See drupal.stackexchange.com/questions/198694/…. What you can use is content.field_text.0, then you only get the inner part of the first field item, without the field templates. That's especially useful for lists, references and so on, where you want the formatted output, not the raw value

@No Sssweat 2017-02-13 07:36:51

@Berdir corrected, thx.

@Berdir 2017-02-13 12:35:36

Better, but still not quite correct. content.field_myfield|striptags doesn't work, you can't pass a render array to that. You would need to render it first and then pass it through that. striptags and so on also have problems with & and other characters that can get encoded twice, so you oftne need a |striptags|raw actually.

@No Sssweat 2017-02-13 12:55:14

@Berdir if true, then it seems pointless to use |striptags at all, might as well just |raw. I will do some testing (with the characters that you mention) with node templates in the next few days or so. According to the official Twig site, |striptags uses the strip_tags PHP function.

@No Sssweat 2017-02-13 13:08:32

@Berdir Also, we are stripping the tags (markup) that is added by Drupal. (well unless they allow to add html tags as the field value, which they should not be doing in the first place, bad practice). But I'll test {{ content.field_myfield|striptags }} to see if it works or not, for now. I will give you the benefit of the doubt.

@Berdir 2017-02-13 13:40:59

Just |raw leaves the html tags and prints them. The problem with |striptags is then double encoding of html entities, not markup, so & becomes &amp; and then &amp;amp;

@4k4 2017-12-27 08:49:47

@NoSssweat, why you've removed getting the value from the entity? This is the best option for a plain text field IMHO, but without the filter |striptags, which you don't need, because .value is the raw value already.

@No Sssweat 2017-12-27 09:06:52

@4k4 I took your suggestion for a test on 8.4.3 and the result shows <p> tags and double encoding &amp;. See this pic, I cleared the cache several times to make sure. I have body field set to Filter HTML.

@4k4 2017-12-27 09:32:29

This is not double escaping, you can see that < and > are displayed correctly. The ampersand is probably already single escaped before you output it. But the question is about a plain text field, not a formatted text field processed by text filters. (To answer this not asked question, the raw value (if someone really wants that) would still be in .value and it would be correctly single escaped by Twig, for security reasons, because you never should output a formatted text field unfiltered)

@No Sssweat 2017-12-27 10:12:52

@4k4 you never should output a formatted text field unfiltered right, but doesn't twig auto-escape them?

@4k4 2017-12-27 10:21:21

Yes, this is what I meant (auto-escaping = single escaping). So your examples for outputting entity field values are perfect, only without the striptag filter, because this would produce the ugly double escaped output. (BTW the &amp; output in your example is wrong, this doesn't happen in autoescaping unless the field content is already escaped one time).

@Andrew Kozoriz 2017-12-27 10:54:20

You can use Twig Field Value module in this case. After install this module You will get access to partial data from field render arrays. For ex.

{{ content.field_name|field_value }}
content.field_image|field_target_entity.uri.value

etc. More info on projects page

Related Questions

Sponsored Content

5 Answered Questions

[SOLVED] Iterate through multiple value content field in Twig template

  • 2016-07-28 16:40:27
  • AngularChef
  • 21173 View
  • 21 Score
  • 5 Answer
  • Tags:   theming 8

2 Answered Questions

[SOLVED] Get value from twig tweak without additional markup

  • 2019-03-18 09:44:31
  • Shawn Matthews
  • 46 View
  • 0 Score
  • 2 Answer
  • Tags:   theming

3 Answered Questions

[SOLVED] Render plain text as HTML in Drupal 8 twig

  • 2015-12-17 10:41:37
  • Alari Truuts
  • 18275 View
  • 15 Score
  • 3 Answer
  • Tags:   8 theming

2 Answered Questions

[SOLVED] How can I access raw field values in a view template?

3 Answered Questions

[SOLVED] Get Views field value in Twig template

1 Answered Questions

[SOLVED] How do I get the plain text value for the {{ label }} field in a twig node template?

  • 2016-11-23 17:47:07
  • flamesquirrel
  • 2639 View
  • 3 Score
  • 1 Answer
  • Tags:   theming 8 entities

1 Answered Questions

[SOLVED] Print raw field data

  • 2012-01-31 09:53:04
  • Andrey Rudenko
  • 1118 View
  • 0 Score
  • 1 Answer
  • Tags:   7 entities

1 Answered Questions

[SOLVED] Display only the value of a field in a content-type template

  • 2016-06-30 20:16:32
  • Matt
  • 853 View
  • 1 Score
  • 1 Answer
  • Tags:   theming 8

2 Answered Questions

[SOLVED] Print Link field in View template

1 Answered Questions

Sponsored Content