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.
{{ 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)
&
output in your example is wrong, this doesn't happen in autoescaping unless the field content is already escaped one time).
Here is the code:
{{ content.field_title|render|striptags|trim|lower }}
it works fine.
{% set parp_state = participating_state_1|render|striptags|trim|lower %} {% if parp_state == "60" %}
&
becomes &
,at least for me
Commented
Aug 8, 2022 at 5:29
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
|field_value
and simply use field templates removing the wrappers. |field_value
tends to circumvent default caching and permission.
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()
Another way is preprocess the plain text through a text_filter. With this method you keep leveraging Drupal's text filters like basic_html. So this way is more secure than rendering the plain text as html in twig.
In this example the field-name is 'field_myfield'. The text-formatter is 'basic_html'. But you could also use 'full_html' or any other available formatter. And it is a paragraph of type 'hero' is preprocessed.
/**
* Implements TEMPLATE_preprocess_HOOK().
*/
function THEME_preprocess_paragraph__hero(&$variables) {
$entity = $variables['paragraph'];
$variables['content']['field_myfield'] = check_markup(
$entity->get('field_myfield')->getValue()[0]['value'],
'basic_html',
$entity->language()->getId()
);
}
And in Twig nothing has changed.
{{ content.field_myfield }}
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 %}
{{ entity.field_name.value }}
will give you either an empty array (if the field is not filled in) or a string (if it is). Therefore, you cannot assume this is a string.
For example, if you pipe it to a function which expects a string, like:
{{ entity.field_name.value|split(',') }}
This might work only if the field has data, but will throw a warning (in PHP 7) or an error (in PHP 8), such as explode() expects parameter 2 to be string
.
Always test your template with empty fields and non-empty fields before using it. Always check your error log , and never assume entity.field_name.value will be a string.
Also, note that this technique will show you the first value in a multi-value field.
See also https://www.drupal.org/project/drupal/issues/3215952#comment-14498809
I have found this useful article from Sarah Carney, in which she explains how to get the values from different field types: body, text, list, link, image, file, boolean, number, email, phone, date, and taxonomy/entity reference.
This is an example that she explains to get the name tags in a ul
(Example 2)
{% if content.field_name[0] %}
<ul>
{% for key, item in content.field_name if key|first != '#' %}
<li>{{ item['#title'] }}</li>
{% endfor %}
</ul>
{% endif %}