Skip to content

Instantly share code, notes, and snippets.

@Alexander-Pop
Last active December 21, 2021 14:15
Show Gist options
  • Save Alexander-Pop/5bb13b389de4c2f6f5196420c37dbb1d to your computer and use it in GitHub Desktop.
Save Alexander-Pop/5bb13b389de4c2f6f5196420c37dbb1d to your computer and use it in GitHub Desktop.
Drupal Set and Get the raw field value in a twig template #drupal8 #d8 #twig
{{ 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
{% set parp_state = participating_state_1|render|striptags|trim|lower %}
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.
In THEME.theme
/**
* 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 }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment