Score:2

Testing equality of two variables - one is entity reference

mk flag

I'm new to Drupal theming and twig, so apologies for asking a simple question, but it's driving me crazy. I can't find the solution.

I'm trying to test the equality of field from a Custom content type - field_program_level - that's an Entity reference type. field_program_level has two possible values : G and UG.

I've tried everything I know and my code doesn't work. My current code is:

{% set yy = content.field_program_level[0]|render %} 
{% set graduate = 'G' %}
{% if yy is same as(graduate) %}
<p> Success! yy was created and equal to {{ yy }} </p>
{% else %}
<p> Failure! {{ yy }} does not = {{ graduate }} </p>
{% endif %}

I'm assuming that because field_program_level is a entity reference it's some type of array or object.

yy = {{ yy }}

outputs "G" as it should, but "is same as" fails.

I tried using {{ node.field_program_level.0.value }} but it doesn't work. I've also tried using filters |render and |trim.

Any suggestions?

Score:1
cn flag

Using the node object instead of the content render array is a good idea, but a reference field doesn't contain a value. You probably want to test against the label of the referenced entity:

{% set yy = node.field_program_level.0.entity.label %} 
{% set graduate = 'G' %}
{% if yy is same as(graduate) %}
Christopher Lambert avatar
mk flag
That did it... thank you. I have to say I'm not fond of Twig.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.