Loading A User's Groups In Drupal 7

Whilst working with Organic Groups today I had the need to load a list of the nodes that a user is connected to. After a bit of looking around in the source code I couldn't find a good solution on how to do this. So after looking around on Google for a bit I just sat down and wrote one.

Getting the group nodes that a user is a member of is quite easy as it turns out, but must be done in a number of steps. The first step is to grab a list of the group entity ID's that the user is connected through using the og_get_entity_groups() function. This can be used with no parameters (which assumes the current user).

// Load in the current user's group entity ID's
$groups = og_get_entity_groups();

Or you can load a user and pass this object to the function.

// Get the current user
global $user;

// Load in the current user's group entity ID's
$groups = og_get_entity_groups('user', $user);

The next step is to use these group entity ID's to load in the actual node entity ID's using the og_load_multiple() function.

// Load the entities object ID's
$groups = og_load_multiple($groups);

We can now finally start loading node objects using these group entities. This can be done in a number of different ways. The first is to loop through them and load in the node objects one at a time.

// Convert them into nodes, one a time.
$nodes = array();
foreach ($groups as $group) {
  $nodes[] = node_load($group->etid);
}

An alternative is to loop through the list of group entities and load all of the node entity ID's into an array. This can then be passed onto the entity_load() function.

// Load all of the nodes with one function call
$nodes = array();  
foreach ($groups as $group) {
  $nodes[] = $group->etid;
}
$nodes = entity_load('node', $nodes);

Using entity_load() is a good alternative as it means that you can use the conditions parameter to only load certain nodes without having to load each node in turn. In the following example I only load nodes of the type 'club' within the $nodes array.

// Load all of the nodes with one function call 
$nodes = array();  
foreach ($groups as $group) {
  $nodes[] = $group->etid;
}
$nodes = entity_load('node', $nodes, array('type' => 'club'), FALSE);

Update:

So it turns out that og_load_multiple() is no longer a part of Organic Groups, at least in the 2.x version. An alternative to this is to use node_load_multiple(), which will load all of the nodes as node objects. So, to load all of the nodes connected to a user in one go do the following:

global $user;
$groups = og_get_entity_groups('user', $user);
$groups = node_load_multiple($groups);

Be careful though, as if a user is a member of lots of groups this code will load all those objects.

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
2 + 11 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.