Search code examples
drupaltwigdrupal-8drupal-field-collection

Drupal 8 field collection – how to get raw value of a taxonomy field from field.html.twig?


I've got a node with a field collection, which contains a taxonomy field. I'd like to get the raw value or ID of this taxonomy field, to use in a conditional statement and provide different markup for different values.

e.g.:

{% set imageType = item.content.field_image_type|raw %}

{% if imageType == 'web-desktop' %}
    // markup A
{% else %}
    // markup B
{% endif %}

The variable works correctly, in that I am getting the expected output when rendering it, however the test always returns false.

What am I doing wrong?

Thanks!


Solution

  • Thanks to @DarkBee for the tip, I dumped the variable and used ['#plain_text'] from the output; the test now works as expected. For reference, here's the code:

    {% set imageType = item.content.field_image_type[0]['#plain_text'] %}