Hi! I'm attempting to get the count for a multiple image field I have on my site and display it programatically.

I'm in the TPL file trying to display the proper number. I've tried the following and they all report "0".

<?php
echo count($row->field_field_images['items']);
echo count($fields['field_images']->content);
echo count($fields['field_images']['#items']);
?>

https://www.dropbox.com/s/ojmb734b0eq28kf/array_count.PNG?dl=0

I attached a screenshot of my Devel array display. I'm trying to display the number of images (which should be 10), on my page.

Could you offer any advice on how to do this? What am I doing wrong?

Thanks!
Brendan

Comments

VM’s picture

in it isn't clear by the above code, the OP is using Drupal 7.

bdparker’s picture

What do you mean? Should I change something? I'm not sure what you mean by OP.

VM’s picture

OP = Opening Post/Opening Poster. You failed to tag the thread with the version of Drupal in use.

bdparker’s picture

It's Drupal 7.

sonona’s picture

In your tpl file do you know which variables you have available? Try using
dpm($fields);
to check if you can even access the $fields array.
You can try
dpm(get_defined_vars());
to see which variables you have.

bdparker’s picture

Odd... I tried that but I don't see a count listed. I see it in Devel, but not here.

sonona’s picture

When you did dpm(get_defined_vars()); did you get the $fields array?

If not then it means you need to pass your theme this array in order to use it.

edmund518’s picture

Try this:

$count = count($node->field_images);

bdparker’s picture

Didn't work. :/ Still reports "0". Thanks, though.

edmund518’s picture

If it is simple field in the node, it should work. I did a small sample my dev site (Drupal 7.x), and it works in my dev.
Please put the code to the node.tpl.php to test.

$node=node_load($nid); //You can pass the node id to it to test.
$count = count($node->field_images);
print $count . " images";

But I closely looked into your array, it seems you need to get the ["field_images"]["#items"] into a new array, then count your new array.

mmjvb’s picture

you do need to get the variable correct!
Doubt very much it is called $fields. I scanned D7 source and $fields is not used in templates.