You are here

function og_get_entity_groups in Organic groups 7

Same name and namespace in other branches
  1. 7.2 og.module \og_get_entity_groups()

Get the groups a content is associated with.

Parameters

$entity_type: The entity type (e.g. "node" or "user"). Defaults to 'user'

$entity: Optional; The entity can be a user, node or any fieldable entity. If empty the current user will be used.

$states: Optional; Array with the state to return. If empty groups of all state will return.

Return value

An array with the group IDs, or an empty array.

11 calls to og_get_entity_groups()
OgGroupApi::testGetEntityGroups in ./og.test
Test the og_get_entity_groups() API function.
OgGroupAudienceField::testHiddenSelectedGids in ./og.test
Test re-adding hidden and selected group IDs.
OgGroupMembership::testGroupMembershipCrud in ./og.test
Test group group membership create, update and delete.
OgGroupMembership::testUserEdit in ./og.test
Test re-saving user with pending membership.
og_access_node_access_records in og_access/og_access.module
Implements hook_node_access_records().

... See full list

1 string reference to 'og_get_entity_groups'
og_group_membership_invalidate_cache in ./og.module
Reset static cache related to group membership.

File

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

Code

function og_get_entity_groups($entity_type = 'user', $entity = NULL, $states = array(
  OG_STATE_ACTIVE,
)) {
  $groups =& drupal_static(__FUNCTION__, array());
  if ($entity_type == 'user' && empty($entity)) {
    global $user;
    $entity = clone $user;
  }

  // Get the entity ID.
  list($id) = entity_extract_ids($entity_type, $entity);

  // Make sure the cached values are according to the states we are looking
  // for.
  // We break down the checks for easier readbility of the code.
  $cache = TRUE;
  if (!isset($groups[$entity_type][$id])) {

    // The value doesn't exist.
    $cache = FALSE;
  }
  if ($cache && empty($groups['__info'][$entity_type][$id]['states'])) {

    // The states are not registered.
    $cache = FALSE;
  }
  if ($cache && count($groups['__info'][$entity_type][$id]['states']) != count($states)) {

    // The registered states are not the equal.
    $cache = FALSE;
  }
  if ($cache && array_diff($groups['__info'][$entity_type][$id]['states'], $states)) {

    // Same number of states but they are not the equal.
    $cache = FALSE;
  }
  if ($cache) {
    return $groups[$entity_type][$id];
  }
  $entity = og_load_entity($entity_type, $entity);
  $groups = array();
  $groups['__info'][$entity_type][$id]['states'] = $states;
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'og_membership')
    ->propertyCondition('entity_type', $entity_type, '=')
    ->propertyCondition('etid', $id, '=')
    ->propertyCondition('state', $states, 'IN')
    ->execute();
  if (!empty($result['og_membership'])) {

    // Get the group ID from the group membership.
    $group_memberships = og_membership_load_multiple(array_keys($result['og_membership']));
    foreach ($group_memberships as $group_membership) {
      $groups[$entity_type][$id][$group_membership->gid] = $group_membership->gid;
    }
  }
  return !empty($groups[$entity_type][$id]) ? $groups[$entity_type][$id] : array();
}