By RobW on
Two related questions:
1. Are filefields and imagefields considered attachments? I'd like to get my terminology straight.
2. Is there a method to count the number of files attached to a node through an unlimited file-or-imagefield? In my case I'd like to count the number of images uploaded to a node through an unlimited imagefield and make that variable available to views for some custom theming.
Thanks in advance for any help.
Comments
Old post. Any solution to
Old post. Any solution to this?
I return to answer myself from the past
The solution is really simple actually.
1. Fields are not attachments. They're fields.
2. If you're working in a tpl or template.php you can use php's count() function on your field array. eg:
If you're working with views, you'll want to look into counting rows (http://drupal.org/node/534714#comment-4085100) or using views_get_view_result(). I prefer the latter.
Thanks for the
Thanks for the reply.
Actually I have no clue as to translate that into D6 syntax.
Can you help me out?
You want to look up
To solve this yourself you'll want to look up a tutorial on working with the node object, sometimes called the node array. It contains all of the information displayed in a node in a php array. Important stuff for working with Drupal. Best place to begin is with a simple theming tutorial and the Devel module.
To answer your question directly:
D6 and D7 have slightly different organization of the node object. To get the value of a field:
in D6:
$node->your_field_name[0]['value']in D7:
$node->your_field_name[$language][0][value]. $language default value is 'und', so you'll see that a lot.If a field in a node has multiple values, they will be stored under an array of delta keys. In a tree it would look like:
0, 1, and 2 are the delta. So to count, you want to specify the array that contains keys you are counting, i.e. the array that contains the delta:
in D6:
count($node->your_field_name)in D7:
count($node->your_field_name[$language]).views
Ok, I've looked into the node structure with Devel (dpm($node)).
And
print count($node->field_photo);works fine in the node.tpl.php file.Now I try to get the same result in Views. I have added a field for the images and grouped the output.
I tried using a Customfield:PHP field that lets me enter PHP code. However, the structure of the view result seems different from that of the node. Could you give an example, please?
EDIT: Aaah. Views stores the results in
$data, it seems. It now works callingprint count($data->node_data_field_photo_field_photo_fid);. Thanks for the help!