Hello,

Would be great to have nodereference count table with views integration, to see how many nodes are referenced to a node per content type.

Comments

frankcarey’s picture

I'm trying to do this with the views_groupby module, which should work, but the node reference relationship is an inner join instead of a regular JOIN. It should be a regular join, so that we can use a group by and get a count.

frankcarey’s picture

Status: Active » Fixed

looks like it is impossible to get a regular join (according to views2 api), so I did it the other way. Listing all nodes, then adding a required relationship to the referenced field. Add the following fields to the view and put them in this order:

1) Title (w/ relationship to node reference)
2) Nid (no relationship, Label:Count)
3) Aggregate SQL: Group by fields (count, Fields to group on: Title, Fields to aggregate: nid)

You should get an output that looks like:
====================
Title | Count
====================
Some Title | 3

frankcarey’s picture

note: downside is that you can't get a zero count this way :(

frankcarey’s picture

Status: Fixed » Active

Yeah, this isn't going to work for me. I'm thinking about doing this pretty much the same way that comment count works, and i think what giorgio79 was suggesting in the first place.

See the {node_comment_statistics} table to see what I mean.

gilgabar’s picture

I had similar needs, so I created a CCK field type to do the counting rather than trying to get Views to do it. It updates the count both when the node being counted is saved as well as when a node referencing the node being counted is saved. You can check it out if you are interested.

http://drupal.org/project/nodereference_count

fgm’s picture

Project: Content Construction Kit (CCK) » References
Version: 7.x-2.x-dev » 7.x-1.x-dev
Component: nodereference.module » Code

nodereference and userreference for Drupal 7.x are now maintained separately in the References module: moving issue over there.

fgm’s picture

Component: Code » Code: node_reference

Categorizing.

danielb’s picture

Just a note that gilgabar's solution of using a separate field could also be done with http://drupal.org/project/computed_field if it is stable enough
Pro tip: Computed Field can save your ass with displaying data about your node in views that isn't already available, although you often duplicate data to make it work, and it does require some PHP knowledge.

IMO a count of items in a view for a particular field is not Node/User Reference specific. It would be useful for any field that allows multiple values.

DamienMcKenna’s picture

Another temporary solution - add a nid field and use code like the following to customize the output (D7):

function MYMODULE_preprocess_views_view_field(&$variables) {
  $view = $variables['view'];
  $field = $variables['field'];
  // Only work on the galleries_list view.
  if ($view->name == 'galleries_list' && $field->field_alias == 'nid' && is_numeric($variables['output'])) {
    $node = node_load($variables['output']);
    $variables['output'] = 'No items added yet';
    // Append the item count of the gallery_items field.
    if (!empty($node->language) && !empty($node->field_gallery_items[$node->language])) {
      $variables['output'] = t('!items items', array('!items' => count($node->field_gallery_items[$node->language])));
    }
  }
}
Stomper’s picture

Is this what you're looking for? http://drupal.org/project/entityreference_count

Seems like its similar to Gilgabar's module except for entities. Can someone please confirm

gilgabar’s picture

Yes, entityreference count is a port of nodereference count that works with all entities, not just nodes.