5

I have been searching online for awhile and so far I have been unable to find any og functions that will give me an array or list of all groups a user is a member of. Does such a function exist? If not, is there a sql query I could run that would give me this?

8

Assuming you are using Drupal 7 and latest OG you can use og_get_groups_by_user().

Get the group IDs of all the groups a user is an approved member of.

  • How can the same thing be done using Views Services Drupal 7 module? – Raj Pawan Gumdal Apr 3 '15 at 8:59
  • Can this be done with a view? I'm not sure how to use that hook or function to make a view. – Christia Mar 15 '18 at 0:38
0

Although I am a big fan of og_get_groups_by_user(), sometimes a bit more control about what is returned with the membership data is useful.

Here's a starter query that'll get you all the group nodes a particular user is a member of:

$uid = 123;

$query = db_select('og_membership', 'ogm');
$query->condition('ogm.type', OG_MEMBERSHIP_TYPE_DEFAULT);
$query->condition('ogm.etid', $uid);
$query->condition('ogm.entity_type', 'user');
$query->condition('ogm.group_type', 'node');

$query->join('node', 'n', 'n.nid = ogm.gid');
$query->fields('n', ['nid', 'title']);
$query->condition('n.type', 'group');
$query->orderBy('n.title', 'ASC');

$rows = $query->execute();
foreach ($rows as $row) {
  drupal_set_message(t('Member of @group', ['@group' => $row->title]));
}
-1

The user groups can also obtained by loading the user. For ex: $test_user = user_load(2); $groups = $test_user['og_user_node']; // returns all group ids of a user

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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