Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By bdparker on
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
=-=
in it isn't clear by the above code, the OP is using Drupal 7.
What do you mean? Should I
What do you mean? Should I change something? I'm not sure what you mean by OP.
=-=
OP = Opening Post/Opening Poster. You failed to tag the thread with the version of Drupal in use.
It's Drupal 7.
It's Drupal 7.
Check available variables
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.
Odd... I tried that but I don
Odd... I tried that but I don't see a count listed. I see it in Devel, but not here.
When you did dpm(get_defined
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.
Try this:
Try this:
$count = count($node->field_images);
Didn't work. :/ Still reports
Didn't work. :/ Still reports "0". Thanks, though.
If it is node, please put the
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.
No need for new array ...
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.