viz12
viz12

Reputation: 725

Using IF & ELSE in Drupal

I have a twig template where I am displaying certain details based on the type of person.
But the condition is not working, I'm just wondering what is wrong with the IF clause?

{% if field_person_type == 'XXXXXXX' %}{{ (content.field_position) }}, {{ (content.field_unit) }}
{% else %} {{ (content.field_position) }}, {{ (content.field_institution) }} {% endif %}

And the content is defined below

Position    field_position      Text            Text field  
Person Type field_person_type   Term reference  Check boxes/radio buttons
Unit        field_unit          Text            Text field  
Institution field_institution   Term reference  Check boxes/radio buttons

When I use dump(field_person_type), it shows following

ARRAY(1) { 
    [0]=> ARRAY(2) { 
        ["TID"]=> STRING(2) "40"
        ["TAXONOMY_TERM"]=> OBJECT(STDCLASS)#179 (8) { 
            ["TID"]=> STRING(2) "40" 
            ["VID"]=> STRING(1) "5"
            ["NAME"]=> STRING(7) "XXXXXXX" 
            ["DESCRIPTION"]=> STRING(0) "" 
            ["FORMAT"]=> STRING(2) "21" 
            ["WEIGHT"]=> STRING(1) "2" 
            ["VOCABULARY_MACHINE_NAME"]=> STRING(11) "PERSON_TYPE"
            ["PATH"]=> ARRAY(1) { 
                ["PATHAUTO"]=> STRING(1) "1" 
            }
        } 
    } 
}

Upvotes: 0

Views: 470

Answers (2)

SBDK8219
SBDK8219

Reputation: 671

Try this field_person_type.0.taxonomy_term.name

Upvotes: 1

VJamie
VJamie

Reputation: 636

Twig cannot access the name magically, you have to access it like you would access an array.

As the dump states: Your field (field_person_type) is an array. Inside you have [0] which is also an array with 2 entries. The "TAXONOMY_TERM" inside is an object so should be accessed as an object.

The following should give you your result

field_person_type[0]["TAXONOMY_TERM"].NAME

Upvotes: 0

Related Questions