7

I have an image field which can contain unlimited values. How can I display the number of image values contained within this field in views?

I'm using Drupal 7.

4

first install computed field module then you should write a for loop within variable which count the value of the field then you just output this variable.

| improve this answer | |
  • 1
    Thx for pointing me to the computed field module. I didn't need to write a loop though. I used count. Computed Code: $entity_field[0]['value'] = count(field_get_items($entity_type, $entity, 'field_images')); Display Code: $display_output = $entity_field_item['value']; After I made this field, I could diplay i'ts value in views. – jeroen Apr 17 '12 at 11:18
11

There is a way of doing this natively in Drupal 7.

  1. Set 'Use aggregation' to Yes
  2. Add a field that contains multiple values, such as a file field
  3. Set the aggregation settings for it to COUNT DISTINCT

Simple. This will however enable aggregation for all fields, but the default settings for other fields will make them behave normally.

| improve this answer | |
2

Good question. There might be a better or built in way to do this, but if not, first check the group multiple values together configuration option in the field you are dealing with and then create a template for that view and field in question:

<?php
$foo=node_load($row->nid);
if (count($foo->field_photo)) {
  print 'There are ' . count($foo->field_photo) . ' photos in this node.';
}

By quickly throwing a view together named foo with a field_photo in it, this would be in a template called views-view-field--foo--field-photo-fid.tpl.php

| improve this answer | |
  • Excellent! No additional module required here! I put your snippet in views-view-fields--MYVIEWNAME.tpl.php – AvL Jun 3 '13 at 9:40
1

Using Views, you want your view to show fields, and your view field will be the delta of the entity field.

'Delta' just means count.

So for instance on my site in the views 'add field' dialog, I see a field called 'Content: File attachments (upload:delta).' Adding this lets me show how many files have been attached to that node.

When you configure this field, you'll probably want to pay attention to the 'no results behavior' section, to hide it if delta = 0 or something.

HTH.

| improve this answer | |
  • is this field the total of all files attached to the node? if so, this works only if he doesn't have any other file fields, right? – Jimajamma Apr 12 '12 at 22:38
  • This view field would the total of all files attached at that field. So you could have other file fields and they wouldn't show. But the question is: how do you show the number of files for one attached file field. – paul-m Apr 13 '12 at 2:23
  • the reason I asked is that in drupal 6 at least, the delta field shows the delta of the individual field, eg, if it has 5 files, it would show 0, 1, 2, 3, 4, one each per row, and not 5, as in the count of them all. so my question is really in drupal 7 is there another delta field that has the count of all of the content in a field, or is there a field that shows how many files are attached to the node as a whole. – Jimajamma Apr 13 '12 at 4:28
  • @Jimajamma you're rigth delta gives oud the position within the array. – jeroen Apr 17 '12 at 11:31
0

Using the Views PHP module I added the Photos/Images field as normal and then added a Global PHP field and added:

$photos = $data->field_field_images;
$count = count($photos);
//dsm($count);
print $count;
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.