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.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communitymyfield 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.
{{ entity.field_name.value }}
to get the true raw value, includes tags and encoding.
{{ node.body.value }}
<p>Batman & Robin</p>
{{ content.field_name.0 }}
to get the raw value minus tags and encoding.
{{ content.body.0 }}
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
<
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)
– 4k4
Dec 27 '17 at 9:32
&
output in your example is wrong, this doesn't happen in autoescaping unless the field content is already escaped one time).
– 4k4
Dec 27 '17 at 10:21
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
None of the suggestions so far worked for my text field. My text field is not multivalue, just a simple text field. Here is what did work for me:
content.field_myfieldname['#items'].getString()
In case this helps anyone, I wanted to only have the start year of a datetime range field displayed, instead of both parts of the range, within a field Twig template.
So I ended up overriding the usual field template with this:
{% if label_hidden %}
{% if multiple %}
<div{{ attributes.addClass(classes, 'field--items') }}>
{% for key, item in items %}
<h3{{ item.attributes.addClass('field--item') }}>{{ element['#items'][key].value|date('Y') }}</h3>
{% endfor %}
</div>
{% else %}
{% for key, item in items %}
<h3{{ attributes.addClass(classes, 'field--item') }}>{{ element['#items'][key].value|date('Y') }}</h3>
{% endfor %}
{% endif %}
{% else %}
<div{{ attributes.addClass(classes) }}>
<div{{ title_attributes.addClass(title_classes) }}>{{ label }}</div>
{% if multiple %}
<div class="field--items">
{% endif %}
{% for key, item in items %}
<h3{{ item.attributes.addClass('field--item') }}>{{ element['#items'][key].value|date('Y') }}</h3>
{% endfor %}
{% if multiple %}
</div>
{% endif %}
</div>
{% endif %}
Here is the code:
{{ content.field_title|render|striptags|trim|lower }}
it works fine.