0

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"

how can I get the length of the array?

  • 2
    Have you tried simply counting it? – Clive Mar 4 '16 at 13:54
1

Confirmed that as suggested in the comments, the following works quite well:

count($node->field_categoria_del_libro_presen);

Here the count would be 2 (based on the example in the question).

This works because even though the field_categoria_del_libro_presen property on the node object is a EntityReferenceFieldItemList, it implements (by way of implementing ListInterface) the native PHP \Countable interface.

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.