0

I'm trying to use two taxonomy items to build a button on my template. The logic is:

If this node is exactly equal to taxonomyTermFromVocabularyA
Print a button on the page with a URL built out of taxonomyTermFromVocabularyB

I thought I was doing well when a simple test proved that my terms were available. In the template, I tested by adding:

<h3>{{ content.field_taxonomy_resource_type }}</h3>
<h3>{{ content.field_taxonomy_topic }}</h3>

And out came the expected values on the page:

Topic Page

Oral Health

However, once I tried to actually use the terms I captured it never worked. The first part of the logic, where the button will eventually be built, I wrote as:

{% if content.field_taxonomy_resource_type == "Topic Page" %}
  <p> hello, it's working! </p>
{% endif %}

produces no content at all. I expected to see my test paragraph sentence "hello, it's working!" appear on the page.

My best google-foo hasn't helped me at all and, alas, my soul is being crushed. I fear it's something completely obvious that I am just ignorant about.

Can anyone point me to the error of my ways? Why doesn't my twig if statement work?

@clive was absolutely right in the suggestion below that I was naming the array and not the particular item I needed when constructing my if statement. His correction to my code:

{% if node.field_taxonomy_resource_type.entity.label == "Topic Page" %}

is not working though, and the best I can tell is that the variable is unavailable to me. When I dump the variable using {{ dump(node.field_taxonomy_resource_type.entity) }}, I just get NULL.

Clive's answer worked as soon as I changed out node to paragraph. Thanks!

{% if paragraph.field_taxonomy_resource_type.entity.label == "Topic Page" %}

3

content.field_taxonomy_resource_type is a render array, which you're comparing against a string. Instead, you need to get access to the label, and extract the term name.

Try this instead (if in a node template):

{% if node.field_taxonomy_resource_type.entity.label == "Topic Page" %}

Or, as we know it's a paragraph template:

{% if paragraph.field_taxonomy_resource_type.entity.label == "Topic Page" %}
  • thank you for the suggestion. It didn't work: {% if content.field_taxonomy_resource_type.entity.name == "Topic Page" %} <p> clive's suggestion </p> {% endif %} didn't produce anything on the page – Leraa Jun 5 '17 at 14:16
  • oo, i was using your earlier suggestion. I'll retry it with "label" instead – Leraa Jun 5 '17 at 14:17
  • Yeah sorry, that's why I commented instead of answering to start with - needed to check – Clive Jun 5 '17 at 14:18
  • still doesn't work on my case. {% if content.field_taxonomy_resource_type.entity.label == "Topic Page" %} <p> clive's suggestion </p> {% endif %} produced nothing on the page. I wonder what I can do to convert an array object to a string that can be compared. Or make my comparison NOT a string? There will only ever be one term from this vocabulary that will be applicable. Can I grab a machine name or an array index or something? – Leraa Jun 5 '17 at 14:20
  • Strange, that's exactly the method core uses (see core/modules/system/themes/test_theme/node.html.twig for example). Have you tried dumping content.field_taxonomy_resource_type.entity.label to see what it contains? – Clive Jun 5 '17 at 14:23
0

I've been looking for something like this as well. I just tested

{% if node.field_taxonomy_resource_type.entity.label == "Topic Page" %}

and it worked for me after a couple of cache refreshes though - I also got null dumps initially when testing.

  • Thank you, I'll try refreshing the cache several times and see if it comes around... What template are you using to test? – Leraa Jun 5 '17 at 16:24
  • My terminal has about 60 lines of nothing but cache clears from all these tests already. I've also done a few rounds of cache clearing from the admin tool bar too, just to shake things up. I see dumps regularly change according to what test I put in them, so I guess it's working. But I really can't explain my NULLS either – Leraa Jun 5 '17 at 16:37
  • It's a custom one, using Zurb Foundation, but not the the contrib theme. – flamesquirrel Jun 5 '17 at 17:03
  • I'm using a node template if that makes a difference. – flamesquirrel Jun 5 '17 at 17:05
  • here's my code {% if node.field_tester.entity.label == 'testing' %} <h2>{{ label }}</h2> {% endif %} – flamesquirrel Jun 5 '17 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.