I was browsing around the new Organic Groups for Drupal 7 and I must say that it is very impressive. Unfortunately, some little functions such as Post Count, Member Count and Join link that were available when OG integrated with Views in Drupal 6.X are no longer available as fields in the current implementation of OG + Views integration (Drupal 7). Is this a bug, something that should pushed to the views module or is there something that I am missing?
Thanks in advance

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

kehan’s picture

I would like the join link functionality, so I'm subscribing here too

John_B’s picture

Is there a workaround to create a member count with Views? It is beyond my skill, but there must be a way! It is an important feature for some types of site.

kehan’s picture

I think I may have worked this out but I haven't tried it yet. Using latest views 7.x-1.x-dev under advanced view settings you can choose group by. I think this can lead to the ability to count things like number of users. You would need to somehow pull in users by the group audience field. I may get around to trying this later today and will let you know how it goes.

idiotprayer’s picture

@kehan,
you may be on to something. I hadn't thought about using views that way for this problem. I'm not so sure of how the counter works in views, but this could eventually cause a performance drag on your query? There has been some mention before of using a counter in an entity field attached to the group. I started trying to work on this, but haven't had time, so I'm left with some very rough code using a seperate statistics table. If anyone is interested, it's a sandbox project at http://drupal.org/sandbox/idiotprayer/1086720. It does work for post count and comment count, however, I couldn't figure out the logic for updating member counts...
@amitaibu, Any hints on how to do this? There doesn't seem to be a d7 version of 'hook_og.' A field_update hook on the group_audience field maybe?

kehan’s picture

Hi @idiotprayer. I'm afraid I didn't get around to doing this today. I would guess that there woudln't be that much perfomance drag on views cos the naming of the function (group by) implies it's a database operation so would be pretty quick.

Let me know how you get on as I'm moving onto a different project next week.

amitaibu’s picture

@idiotprayer,
I think porting og-statats should be done in a different way -- there is no need for custom tables. The count information can be added to fields that are attached to the group node.

idiotprayer’s picture

Agree, that is the better way, just a bit trickier for me. I will revisit the code I had for that version of the port.
Any suggestions regarding the strategy for member count?

amitaibu’s picture

Getting members/ nodes count is almost the same, you need to query the og_membership entity:

    $query = new EntityFieldQuery();
    $result = $query
      ->entityCondition('entity_type', 'og_membership')
      // Type can be "node" or "user"
      ->propertyCondition('entity_type', $entity_type, '=')
      // This is the group ID
      ->propertyCondition('gid', $gid, '=')
      // Get only the active members
      ->propertyCondition('state', OG_STATE_ACTIVE, '=')
      ->count()
      ->execute();
cartagena’s picture

Can you please tell me where I need to put this query to get a view that displays a list of groups showing number of members in each group? Thank you.

cartagena’s picture

John_B, I am posting of screen shot of my views settings that give me a member count by group. I still hope to learn how to make adjustments with code and queries as in #9 below but for now, this works. Hope it helps you!

rsanan-1’s picture

@cartagena Can you explain how you achieved that ?

cartagena’s picture

@rsanan, I set it just as the screenshot of my view shows. Can you be more specific in your question? I am not a programmer so I stay within the parameters of existing modules like views and og.

BeaPower’s picture

I wasn't able to see the COUNT field in fields... and I have relationships set up as you have them in the pic.

cartagena’s picture

BeaPower, I am working on rebuilding this to give you an answer but had a family emergency. Hopefully later this week!

BeaPower’s picture

ok, hope all is well!

cartagena’s picture

I'm sorry but I can't recreate what I had. I decided not to use it months ago, but I kept the original so I went back to it so I could give you clearer instructions. I must have changed something along the way because I can't get the member count to work properly. I did try but to no avail. I was also using a very new version of OG for Drupal 7 when I got the count so something may have changed in later versions, too.

mototribe’s picture

mototribe’s picture

Version: 7.x-1.0-alpha1 » 7.x-1.1
Component: og.module » Og Views
Category: task » bug

I found a bug:
- when I try to order the above view by the member count I add field "og membership: entity id" and select "count distinct" and then get the error
"The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item."

drew29’s picture

Hello,

I've try to configure out my vie but I don't become any results only the labels will be output.

The steps are:

create a view for "content" type "group". Use "fields" and show "content:title", that's the name of your group
add relationship "Group: Node group" (required), then "(group) Organic groups group: OG membership" (required).
add filter "(OG membership) OG membership: Entity_type " and set to "is equal" to "user"
"use aggregation"
add field "(OG membership) OG membership: Entity id" and use "count distinct" option. That's your count of members.

If you want the count of nodes that are "linked" to a group you would just change the filter from "user" to "node".

The first and second step I understand but the third step where can I put "user" I have only a textfield

can you explain me how can I solved it :-)

kind regards
drew

drew29’s picture

Hej,

I'am again. I have found a solution on http://drupal.org/sandbox/idiotprayer/1086720

and you need the module "Organic groups Views integration" and it works perfect for me :-)

hope it helps

kind regards
drew

qpro’s picture

FileSize
54.34 KB

This another way to show a group list with member count field only with Views and Organic Groups modules.
I hope it helps someone , I were struggling my mind to find it.

1 - Create a view showing users ( adding user:active filter criteria )

2 - Add relationships: first "OG membership: OG membership from User" and then "(OG membership from user) OG membership: OG group gid"

3 - Add fields: first "(OG group) OG group" and "COUNT(User: Name)" for this you have to enable "Use aggregation"

Image attached

BeaPower’s picture

@22 - Hi do you mind exporting the view to us, because I was not able to replicate the fields, thanks.

qpro’s picture

FileSize
59.77 KB

Few days later I realize that a method like #18 is better. I did few changes:

  1. Create a view create a view for "content" type "group"
  2. add field "content:title" and select "table" style for a more comprensible view
  3. add relationship "OG group: OG group from Nodo"
  4. add "(node being the OG group) OG group: OG membership from OG group"
  5. add "(OG membership from OG group) OG membership: Usuario/a from OG membership"
  6. activate "Use aggregation
  7. add field "User:uid" and in its "aggregations settings" select "count distinct" , it would look like as "(user from OG membership) COUNT(DISTINCT User: Uid)"
BeaPower’s picture

@ #24 -Sorry, but I'm using the english version of drupal. So I don't see in relationships - OG group: OG group from Nodo or any of the other relationships you listed - node being the OG group) OG group: OG membership from OG group.

willkaxu’s picture

Hi, guy. #24 means OG group from Node. You can check it in your view.

qpro’s picture

Hi

  • I am using the dev version of organic groups, i think some names changed from the stable release http://ftp.drupal.org/files/projects/og-7.x-1.x-dev.tar.gz
  • I had changed the language to english to make the screenshoot but It seems it didn´t work properly:
    Nodo means Node , Usuario means User , almost everything else is in English.
BeaPower’s picture

Ok, I switched to the dev version and this worked, thanks!

willkaxu’s picture

Version: 7.x-1.1 » 7.x-2.x-dev

Some relationships in #24 can't be found in 7.x-2.x-dev, all the relationships are whole new in 7.x-2.x-dev.

tomogden’s picture

Updating to the 2.x-dev version created major havoc on my site, similar to at least on other person. See #1423278-9: Column not found: 1054 Unknown column 'group_type'.

sunwukong’s picture

subscribe

qpro’s picture

7.x-2.x-dev version deprecate "OG group entity" and now OG depends on EntityReference which simplifies the relationships and the way to build views.

See the discussion at http://drupal.org/node/1342632 and my efforts to understand the new panels integration with in http://drupal.org/node/1431246.

sal-h’s picture

FileSize
168.75 KB

After messing around with this problem for a day and a half, I finally came up with the solution for a group member count.

  1. Create a content view with the content type of your group
  2. add a relationship -> filter by Group -> select Group: Node group
  3. add a relationship -> filter by OG Group -> select OG group: OG membership
  4. Set "Use aggregation" to Yes
  5. add a filter criteria -> filter by OG membership -> select OG membership: Entity_type -> group results together -> set the value equal to user
  6. add a field -> filter by OG membership -> select OG membership: Og membership ID -> on aggregation type select Count DISTINCT

Attached is a screen shot of my view configurations. Enjoy!

nedjo’s picture

Sample query for fetching the number of members in a given group with specific roles:

$gid = 4;
$rids = array(
  7,
  9,
);
$count = db_select('og_users_roles', 'our')
  ->fields('our', array('uid'))
  ->condition('rid', $rids, 'IN')
  ->condition('gid', $gid)
  ->execute()
  ->rowCount();
allio_froggio’s picture

These instructions worked like a charm. Thanks for taking the time to post them, #33!

Sinan Erdem’s picture

Easiest way if you are already using members view to show the group members in a block:
http://drupal.stackexchange.com/questions/25976/return-a-views-total-row...

seb.pisarski’s picture

Hi guys, the easiest way to display members and posts counter (without aggregation, contexts etc.) is to use views php with the following pieces of php code:
1. $posts = og_membership_load_multiple(FALSE, array('gid' => $row->gid, 'entity_type' => 'node'));
print count($posts);
2. $members = og_membership_load_multiple(FALSE, array('gid' => $row->gid, 'entity_type' => 'user'));
print count($members);
Your view needs to have group id column (gid).

modctek’s picture

Subscribing - I could not get either #33 or #37 to work. Feeling like a failed web geek at the moment. :(

#33 gave me a SQL syntax error and #37 just returned "0" for every group.

seb.pisarski’s picture

FileSize
123.94 KB

#38 few details on my view setup:
1. in the relationships section I added "OG group: OG group from Node";
2. added "OG group: Og group ID" field via relationship (1.) and positioned it higher than (3.);
3. installed "Views PHP" and added "Global: PHP" field with the following code in Output code field:

 $posts = og_membership_load_multiple(FALSE, array('gid' => $row->gid, 'entity_type' => 'node'));
 print count($posts);

I hope this helps.

drupalycious’s picture

I tried the method #33 but now only "OG membership: OG membership from Node" is available. And the method doesn't work.

Did someone find out how to expose the number of members and posts per group in a view, with the latest dev of OG?

Thanks

Taxoman’s picture

Category: bug » feature
PlayfulWolf’s picture

can confirm, that none of those method work for me on OG 7.2

jacobpov’s picture

Bump I really need this , this is the most basic and needed thing for UI!

qpro’s picture

Finally my solution was:

1 - Include "Global : Result Summary" field in the header of some of my organic groups views to show the number of total items.
2 - Use filters to adjust the result , for example: only active users , only published content , etc...

PlayfulWolf’s picture

@qpro: about which kind of view are you talking?

Did you made list with 7.2 like: http://drupal.org/files/results.jpg ?

qpro’s picture

The changes described in #44 was made in the view "OG members" included in the module (version 7.2)

tmsss’s picture

I was trying to get the group member count and #39 did the work for me, just changed 'entity_type' => 'user'.

Thanks!

Yuri’s picture

@qpro #46, can you elaborate on your method please, I don't understand how you filter the results of the summary field in the header. And I guess you were not able to use that method to show count in fields

Yuri’s picture

@seb.pisarski #39 and @dissid3nt: #47 Am I right that you are NOT using OG 7.x-2.x (since that is what this thread is about) ?

amitaibu’s picture

Status: Active » Fixed
FileSize
54.94 KB

Note that we already have Post Count & Member Count as a views plugin for a few weeks now.

PlayfulWolf’s picture

1) how to see it? I cant find.
2) How to make list with views:
Group A - 23 posts, 5 members
Group B - 523 posts, 93 members

amitaibu’s picture

PlayfulWolf did you look at the screenshot in #50?

PlayfulWolf’s picture

I did, but maybe I am a bit slow :( Ok, I will play with this in a few days and will not waste your time.
If OG 7.2-dev has functionality to display node and member count in views list - this request is closed.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

PlayfulWolf’s picture

Status: Closed (fixed) » Active

I am going to reopen this issue, as there are no built in OG functionality to buid sortable group list with members and posts count, like in #45:

Group A - 10 members, 100 posts
Group B - 5 members, 50 posts

If it is supposed to be generated by views with aggregation settings - let that be.

drupalshrek’s picture

FileSize
58.6 KB

The problem I have is pretty similar to the problem I read here.

In Drupal 6, when I edit my OG view, e.g. /admin/build/views/edit/og then when I do "Add fields" I have the option to select from a whole lot of fields grouped under "Organic Groups" such as:

Organic groups: Group: Members count
Organic groups: Group: Post count
Organic groups: Group: Join link

Attached is a screenshot which illustrates the Drupal 6 view.
screenshot of OG view in D6

In Drupal 7, I don't see where I find the ability to get the same items. What I want is a view which shows a list of all groups and the above 3 items for each group. I can get a list of groups with their title (since this is a just a list of node titles, where the node is of a given type), but I cannot figure out how to get the above 3 items.

Thanks for any help.

PlayfulWolf’s picture

It is possible to include "Join link" in a list like this - the best way to get an example is to install OG extras module and take a look included views.

However I still cannot find a solution to show members/posts. It may be simple with rules incrementing some custom field, but this is above my knowledge and I cannot get help on rules and og groups

PlayfulWolf’s picture

@ Amitaibu - please, explain for us, poor incompetent OG users, how to make a view with members & post count. I cannot get any of the list like (I guess like the other 31 of followers of this issue).

Group A - 10 members, 100 posts
Group B - 5 members, 50 posts

I have looked at 50#, but this is just group overview page, not a view of groups.

Please...

amitaibu’s picture

> @ Amitaibu - please, explain for us, poor incompetent OG users, how to make a view with members & post count

Either you have fields on the group node, that accumulate the number of posts/ users every time there's a new one; or either you'll have to write some custom code to show the desired list. Not sure if Views would help you for the 2nd scenario.

nksahu’s picture

I did it in a different way

1) create a view of users
2) Change the format to Table ( Now you see all the users )
3) Added a Relationship -> OG membership: OG membership from User
4) Add another relation ship ofcourse using the previos relationship --> (OG membership from user) OG membership: Group Node from OG membership
5) Now Add Field -->Content :Title and use the relationship --> (Group node from OG membership) Content: Title (Title)
This should give the name of the group
6) Now in OTHERS click Use aggregation
7) Now use count of USERNAME
--Done--

asherry’s picture

The main thing I don't like about the aggregation solution is that you can't have different types of membership counts at the same type. For example if you have a view of group nodes, you could potentially have a user membership, and a node membership. There should be the ability to see both of those counts and not have to have conflicting left joins.

I've proposed something in the og_extras module, I'd like to get some feedback. We can create a new field in a view that you customize the target entity for, and the field would be able to attach to any entity view.

I have a patch I'm in the process of testing out.

http://drupal.org/node/1968372

IshaDakota’s picture

In order for the aggregation solution of membership and post counts to work on a view of groups you'll need the following:

Assuming OG 7.x-2.x, create a view of the group content type with a table format

For members count:
1. Add the relationship "OG membership: OG membership from Content group."
2. Add the relationship "OG membership: User from OG membership" - in the configuration, set Relationship to "OG membership from node group" and make sure the Identifier is set to "user from OG membership."
3. You should now have 2 relationships listed: "OG membership: OG membership from Content group" AND "(OG membership from node group) OG membership: User from OG membership."
4. Add the field User: Uid and in the configuration, use the relationship "user from OG membership"
5. Turn aggregation on.
6. In the aggregation settings for the User: Uid field, set to COUNT.

For posts counts:
1. If you haven't already (when doing members count), add the relationship "OG membership: OG membership from Content group."
2. Add the relationship "OG membership: Content from OG membership" - in the configuration, set Relationship to "OG membership from node group" and make sure the Identifier is set to "node from OG membership."
3. You should now have another relationships listed: " (OG membership from node group) OG membership: Content from OG membership."
4. Add the field Content: Title and in the configuration, use the relationship "node from OG membership"
5. If you haven't already, turn aggregation on.
6. In the aggregation settings for the Content: Title field, set to COUNT.

This is probably not the most performant solution - if you have many groups, members, and posts there could be a performance hit.

@asherry - I tried your patch to OG Extras and it worked for me (for members counts, couldn't figure out how to get post counts). Can't yet really comment on the method as I haven't looked closely at the code.

fadgadget’s picture

Ive been trying to do this for months and have failed miserably every time. I was hoping that Isha's post above was going to save me but as before 'i have failed miserably'. My failings started at step 1.

OG membership: OG membership from Content group

I dont have this relationship. I am using the latest dev version. Relationships i do have that look similar are-

OG membership from Node
OG membership from Node group

I suspect its one of them. Lets try them both again then....

actually neither lets me have the relatiosnhip in step OG membership: Content from OG membership

OG membership: Node from OG membership seems about as close as i can get. Im going to have to give up. Ive a feeling its 'that easy' to do now that its slipped under my radar. Is there any reason why i might not have a OG membership: OG membership from Content group relationship? Is it possibly just called something else? thanks

jh81’s picture

FileSize
57.03 KB

I'm having the exact same issues as fadgadget in #63, I don't see

OG membership: OG membership from Content group

I've attached what I do see. This should be simple to accomplish but I have spent hours on it and can't figure it out. Any help would be greatly appreciated.

PlayfulWolf’s picture

this feature is standing so long, I can chip-in 5-20$ for solution: it does not matter in which module, og, og extras, og stats or another.

IshaDakota’s picture

@fadgadget and @jh81, did you try following the instructions in #62, substituting the word "Node" for "Content." I don't know where that label is generated, but maybe in the dev version it was changed (I am using 7.x-2.2), as "Node" is actually more accurate than "Content"

If you do it, post a screenshot of the view configuration with the "Advanced" section expanded - maybe that will provide more troubleshooting info.

jh81’s picture

Version: 7.x-2.x-dev » 7.x-1.5

@IshaDakota, I'm using OG 7.x-2.2 and was finally able to get this working replacing "Content" for "Node". My relationships are for users:

OG membership: OG membership from Node group
(OG membership from node group) OG membership: User from OG membership

and for content:

OG membership: OG membership from Node group
(OG membership from node group) OG membership: Node from OG membership

fadgadget’s picture

wowsers all working now Isha thanks (and jh81) Ive been trying to achieve this for months. excellent :)

AaronBauman’s picture

Title: No count functionality in OG 7 » Count functionality in OG 7 is not obvious or intuitive

#62 accurately answers OP.
Unfortunately this answer is neither obvious nor intuitive for those of us not intimately familiar with OG's database schema.

This is a pretty common thing to want to do.
How can we make this easier?

PlayfulWolf’s picture

https://drupal.org/node/1070324 - aaron, perhaps this thread can be as a continuation to your question

AaronBauman’s picture

Status: Active » Closed (won't fix)

Yeah, I guess it makes sense for this to live in contrib.
Unless someone can provide a compelling reason otherwise, you'll need to check out a contrib module like "OG Statistics" if the tedious process in #62 does not suit you.

#1070324: Port Organic Groups Statistics to OG 7

fadgadget’s picture

sorry if this is the wrong place to ask this but it seems related. Everything went well following #62 instructions. I have just noticed though that only User 1 can see the User Totals. It shows as 0 for normal members. the Node totals works fine for all though.

Can anyone think of a permission that i might have to change for members to see the aggregate User totals?

thanks

IshaDakota’s picture

Category: feature » support
Status: Closed (won't fix) » Active

@fadgadget I am having the same problem and can't seem to find the right solution.

If you give users the Node permission: "Bypass content access control" (admin/people/permissions), they can see the user count. Obviously this is not a workable solution.

I can't seem to find the og permission that affects this.

AaronBauman’s picture

Did you try changing the views query settings to disable sql rewriting?

fadgadget’s picture

I wont be able to aaron im afraid as i have private groups that i dont want people to see on the list. Maybe i could just get rid of the user count. i think folks will be more interested in the amount of content than other users on my site. thanks for the suggestion though.

fadgadget’s picture

i suppose a workaround for me could be to add a select checkbox to my group content type and then have the list only show groups with the checkbox clicked. Ill just need to make private groups private via this checkbox. let me go see if it works..... yeah that seems to work fine aaron thanks for the tip.

IshaDakota’s picture

Yes aaron, disabling SQL rewriting appears to be the correct approach. Thanks as well for that tip.

My only remaining question to someone more familiar with DB queries and performance is: Is this a scalable solution? Obviously it's not infinitely scalable, but at what point would performance suffer? 100s of groups? 1000s of posts?

The approach in #1070324: Port Organic Groups Statistics to OG 7 and #1968372: Membership counts and membership lists is to add fields that store counts of members and posts. This is clearly easier to handle from a query perspective, but it seems no one has come up with the completely developed solution.

seb_x’s picture

Now migrating to OG 7.x 2.x My code in #38 / #39 works fine, see the recipe below:

1. Instal OG 7.x 2.x + Views PHP 7.x 1.x
2. Create view for content:
- relationships: OG membership: OG membership from Node (not required)
- filter: content published of group type
- fields:
a) content: nid (hidden)
b) content: title
c) global: PHP (this one is for posts counter)
- output code:
$posts = og_membership_load_multiple(FALSE, array('gid' => $row->nid, 'entity_type' => 'node')); print count($posts);
d) global: PHP (this one is for members counter)
- output code:
$posts = og_membership_load_multiple(FALSE, array('gid' => $row->nid, 'entity_type' => 'user')); print count($posts);

Voila, I hope it helps somebody.

drummondf’s picture

Thought I would make a note that #62 worked great for me, thanks! Using 7-x-2.3

rclemings’s picture

Both #62 and #78 work for me as far as getting post counts. I went with the latter only because it involved less typing.

But does anyone have an idea on how to get counts of *unread* posts? That was another field that used to be available directly but isn't now.

I can use the "has new content" field, but that's just a flag, not a count.

drummondf’s picture

@rclemings how are you identifying "unread" posts? Unread by who? Maybe defining that a little better will help us figure out which filter is most appropriate

rclemings’s picture

"Number of new posts in a group for the current user" is how OG defined the now-missing field previously. I'm referring to OG v6.x-2.4. It's defined in og_views.views.inc.

Anonymous’s picture

I have seen many custom code posts around the web forget to properly query the database. When you query the og_membership table, you must not forget there might be attached nodes that allso have an og_membership. Hence, you must limit your queries to entity_type user, when you are counting users, and entity_type node when you are counting attached nodes. Here are some sample snippets that someone might find useful (I'm using Drupal 7)

Return count of all the users in group with gid 777

$givengid=777;
$query = "SELECT COUNT(*) amount 
FROM {og_membership} o "."WHERE o.gid = :gid AND entity_type='user' ";
$result = db_query($query, array(':gid' => $givengid))->fetch();
$valued=$result->amount;
return $value;

Return a count of all the groups of use with uid 888

$givenuid=888;
$query = "SELECT COUNT(*) amount 
FROM {og_membership} o "."WHERE o.etid = :etid AND entity_type='user' ";
$result = db_query($query, array(':etid' => $givenuid))->fetch();
$valued=$result->amount-1;
return $value;

If you want to do modify your queries to do something similar, just have a look at the og_membership table and see what you have to modify. You should be able to figure it out, by substituting the appropriate parts in the codes above.

Hope this is useful to someone.

Shabbir’s picture

Issue summary: View changes

Hi,

@PlayfulWolf, in your comment #57 you said, you can add join link by installing og_extras module. I did install og_extras module, can you please be a bit more clearer on what to do in order to get the join link visible..

Thanks,
shabs

mesnitu’s picture

@Shabbir, with og_extras you have a block with the join link. Or if you use pages, you have a view called Content create links

divined’s picture

 $posts = og_membership_load_multiple(FALSE, array('gid' => $row->nid, 'entity_type' => 'user')); 
 print count($posts);

Always return 0.

$row->nid != gid.

How to recieve gid from node? Not in view, i.e. node.tpl ?

moin khan’s picture

friends, 62 works perfectly but total users count field displaying (0) to other site members but when i login as a admin, user count field displaying properly with numbers.