Node gallery: display image count in a template file I use the Node Gallery API  module (part of the node_gallery project) to create galleries of nodes. Now I need to theme node--gallery.tpl.php to display the count of images in a gallery.

I tried to use the Node Gallery API function called node_gallery_api_get_item_count, but it doesn't work. So I tried to look at the database and indeed the node_gallery_galleries table contains zero values on the item_count and pub_item_count columns (even if I call the function with reset=TRUE).

So how to calculate the value directly from the database?
One answer:

&lt;?php
  $results = db_select('field_data_node_gallery_ref_1')
  -&gt;fields(NULL, array('entity_id'))
  -&gt;condition('node_gallery_ref_1_target_id', $node-&gt;nid)
  -&gt;execute()-&gt;fetchAll();

  $pcount = count($results);
?&gt;


Explanation:

The field_data_node_gallery_ref_1 table contains one row for each picture in the gallery. I filter it by the nid of the gallery (node_gallery_ref_1_target_id) in order to get only pictures in that gallery, then count how many pictures exist on the gallery by counting the results of the query array.