How to show a number of group members next to each OG group in Drupal 7

1.1k Views Asked by At

I have a view showing all the OG groups. Now, I'd like to display a number of members and posts within the group next to each group name. I have spent last few days looking for a solution for my problem, unfortunately with no luck. This would look like this:

Group name - Members: X Posts: Y

I tried to create separate views showing list of groups (this was easy) and number of members of each group (easy) and then use views_field_view module to display the number of members as a field under the group name. This almost worked. The problem is that each group shows now a multiple times on the list. I will be rally grateful for any direction or advice. Cheers!

2

There are 2 best solutions below

1
pete80 On

The easiest thing to try with your current setup is under "Advanced" > "Query settings" select distinct on the first view.

If this dosnt work, instead of using views_field_view just output the group ID in the first view and then theme this field in a views template file to bring in your other view setting the group id as the argument to this view.

Here is an example, put the following code in a file called:

views-view-field--view1name--groupidfieldname.tpl.php

changing the groupid, view and display names.

<?php
$view = views_get_view('view_2_name');
$view->set_display('display_name');
$view->set_arguments(array( $row->node_field_data_field_groupidfieldname ));
$view->execute();
print $view_leader->render();

?>

1
Steff On

I think your answer is here: https://drupal.org/node/1101102#comment-7404856

You could also use https://drupal.org/project/views_php .There you can access the group id and query the members count and many other funny things.