Last updated March 17, 2011. Created by whimsy on August 11, 2010.
Edited by Pomliane. Log in to edit this page.
I have a content type containing a multi-value image field (field_images). The number of images that can be added is unlimited. I'm using the Computed Field module which allows me to use php code to calculate the number of images that have been uploaded and display that total in a view.
When using the Computed Field module, put this code in the Computed Code field:
if (is_null($node->field_images[0]['fid']))
{
$node_field[0]['value'] = 0;
}
else
{
$node_field[0]['value'] = count($node->field_images);
}Put this in the Display Format field:
$display = $node_field_item['value'];Initially, I only used the count function without the preceding if statement. This did count correctly for nodes having at least one image; however, it would return a value of 1 for any node with no images. By looking at the database table, I figured out that when I add a node that has the field_images field, a row is created in the content_field_images table even if the node has no images. If there are no images, the field_images_fid field will be NULL. If you only count $node->field_images, the code will always return a minimum value of 1 because of that row in the table, even though it has a null value. So I had to first test for nodes with NULL in the field_images_fid field and return 0 for them. Hope this helps somebody else. I'm not a coder, so it took me two days to figure this out.
Comments
PROBLEMS WITH YOUR CODE
I HAD PROBLEMS WITH YOUR CODE.
ANYWAY I SUCCEDED IN OBTAINING THE RESULT WITH THIS CODE:
-------------------------------------------------------------------------
$node_field[0]['value'] = count($node->field_cliproadr[0]['nid']);
-------------------------------------------------------------------------
Did not work for me (last field empty, non null)
This code did not work for me. I have two problems:
To get around these problems, I changed the code to:
$count1 = count($node->field_images);
$count2 = 0;
for($i = 0; $i < $count1; $i++) {
$fid = $node->field_images[$i]['fid'];
$n = is_null($fid) || $fid == 0;
if (!$n) {
$count2++;
}
}
$node_field[0]['value'] = $count2;
This worked for me. Maybe other people will find this interesting as well.
Helped!
CCK Computed Field + this code
$node_field[0]['value'] = count($node->field_images);solved my case.If i was a PHP coder I think I could solve it with views — but I'm not.
Thanks s lot.
Does this work for D7?
Which code works also for D7?
This worked for me in D7
Computed code:
$entity_field[0]['value'] = count(field_get_items($entity_type, $entity, 'field_images'));Display Code:
$display_output = $entity_field_item['value'];Don't use count()
Overhauling an old site reminds me of a warning, for any who might still care...
Calling count() on the field simply counts all of the items in the array - including any empties. You must check for a positive fid; here is a very clean and paste-friendly method of computing this in Drupal 6:
$node_field[0]['value'] = 0;foreach ($node->field_images as $v) if ($v['fid']) $node_field[0]['value']++;
Helped
I ammended the field names in original to reflect mine and worked like a charm. Thanks for life saving.