2

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?

1 Answer 1

1

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) %}
1
  • That did it... thank you. I have to say I'm not fond of Twig. Jan 13, 2023 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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