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

1.7k Views Asked by At

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!

1

There are 1 best solutions below

0
On BEST ANSWER

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'] %}