shevatvb
shevatvb

Reputation: 31

Drupal 8 get value of a custom field that has multiple value

I'm developing a site with Drupal 8. I made a custom content type, with a lot of fields; in particular I have a field called "field_categoria_del_libro_presen" that is linked to a taxonomy term. It's possible to specify more than one value. I used kint() to obtain the structure of the node. Here: field_categoria_del_libro_presen
→array(2) target_id

1

"1"

2

"4"

I attached the screenshot too.

screenshot How can I get the value "1" and "4" that represent the terms of my taxonomy? Thank you so much in advance. Regards. Valentina

ANOTHER QUESTION UNSOLVED: how can I get the length of the array?

Upvotes: 2

Views: 3335

Answers (2)

Frank Drebin
Frank Drebin

Reputation: 1083

According to your self provided answer I guess you're talking about how to get the value in a twig template. Well generally speaking if you want to get the length of an array you can use the "length" filter. It looks like this:

{{ someArray|length }}

And regarding your original question, if the number of selected values varies and you want to display them all I would suggest using a for loop, it looks like this:

{% for arrayElement in someArray %}
    {{ arrayElement.someKey }}
{% endfor %}

For more information look here: http://twig.sensiolabs.org/doc/tags/for.html

and here: http://twig.sensiolabs.org/doc/filters/length.html

Upvotes: 0

shevatvb
shevatvb

Reputation: 31

I found a solution. So I answer my question. {{ node.field_categoria_del_libro_presen.0.target_id}} {{ node.field_categoria_del_libro_presen.1.target_id}}

Upvotes: 0

Related Questions