1

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!

  • How are you verifying the variable? Did u try `{{ dump(imageType) }}` – DarkBee Oct 13 '17 at 18:10
  • Thanks for the reply; have just dumped, and it seems the returned value is an array. Here's the output: `array(2) { ["#plain_text"]=> string(11) "web-desktop" ["#cache"]=> array(3) { ["tags"]=> array(1) { [0]=> string(16) "taxonomy_term:67" } ["contexts"]=> array(1) { [0]=> string(16) "user.permissions" } ["max-age"]=> int(-1) } }` – Hanin Hamed Oct 13 '17 at 18:29
  • And that is where the answer lay! Thanks for the tip. – Hanin Hamed Oct 13 '17 at 18:34

1 Answers1

1

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