So I simply want to count the number of tags on a term reference field.. I've tried a count (below) and it simply returns "1" (there are a lot more terms tagged than 1):

$entity_field[0]['value'] = count($entity->field_termref);

Also something like below does not work either and also returns "1" - I'm thinking I have to loop through the multiple tags somehow but I'm not sure how to do it. Thanks a lot for any help.

$myterms = array();
foreach($entity->field_guests as $tmp) {
  $myterms[] = $tmp_field_term;
$entity_field[0]['value'] = count($myterms);

Your first code was correct as far as it went: count($entity->field_termref) will return a count of one because the array has only one item, the list of terms.

To count the members of the list your will need to drop down into the array to actually count the list:

$entity_field[0]['value'] = count($entity->field_termref[LANGUAGE_NONE]);

The members of the list can then be counted:


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.