You are here

function og_get_groups_by_user in Organic groups 7.2

Same name and namespace in other branches
  1. 7 og.module \og_get_groups_by_user()

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

Parameters

$account: (optional) The user object to fetch group memberships for. Defaults to the acting user.

$group_type: (optional) The entity type of the groups to fetch. By default all group types will be fetched.

Return value

If $group_type is provided then an array of group IDs matching the specified group type. If $group_type is not provided then an associative array is returned containing arrays of group IDs keyed by group type. If no results are found an empty array is returned.

2 calls to og_get_groups_by_user()
OgSelectionHandler::buildEntityFieldQuery in plugins/entityreference/selection/OgSelectionHandler.class.php
Build an EntityFieldQuery to get referencable entities.
og_plugin_argument_default_user_groups::get_argument in includes/views/handlers/og_plugin_argument_default_user_groups.inc
Get the default argument.

File

./og.module, line 3575
Enable users to create and manage groups with roles and permissions.

Code

function og_get_groups_by_user($account = NULL, $group_type = NULL) {
  if (empty($account)) {
    global $user;
    $account = $user;
  }
  $gids = array();
  if (!og_get_group_audience_fields()) {

    // User entity doesn't have group audience fields.
    return $gids;
  }

  // Get all active OG membership that belong to the user.
  $wrapper = entity_metadata_wrapper('user', $account->uid);
  $og_memberships = $wrapper->{'og_membership__' . OG_STATE_ACTIVE}
    ->value();
  if (!$og_memberships) {
    return $gids;
  }
  foreach ($og_memberships as $og_membership) {
    if (!empty($og_membership)) {
      $gids[$og_membership->group_type][$og_membership->gid] = $og_membership->gid;
    }
  }
  if (isset($group_type)) {
    return isset($gids[$group_type]) ? $gids[$group_type] : array();
  }
  return $gids;
}