Loading A User's Groups In Drupal 7

22nd May 2012 - 4 minutes read time

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).

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

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

  1. // Get the current user
  2. global $user;
  3.  
  4. // Load in the current user's group entity ID's
  5. $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.

  1. // Load the entities object ID's
  2. $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.

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

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.

  1. // Load all of the nodes with one function call
  2. $nodes = array();
  3. foreach ($groups as $group) {
  4. $nodes[] = $group->etid;
  5. }
  6. $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.

  1. // Load all of the nodes with one function call
  2. $nodes = array();
  3. foreach ($groups as $group) {
  4. $nodes[] = $group->etid;
  5. }
  6. $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:

  1. global $user;
  2. $groups = og_get_entity_groups('user', $user);
  3. $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.