You are here

function og_get_entity_groups in Organic groups 7.2

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

Get the groups an entity is associated with.

Parameters

$entity_type: The entity type. Defaults to 'user'

$entity: (optional) The entity object or entity ID. If empty, and $entity_type is "user", the current user will be used.

$states: (optional) Array with the state to return. Defaults to active.

$field_name: (optional) The field name associated with the group.

Return value

An associative array keyed by the group's entity type. Each value is itself an array, where each item has for its key the OG membership ID and for its value the group ID.

24 calls to og_get_entity_groups()
OgBehaviorHandler::delete in plugins/entityreference/behavior/OgBehaviorHandler.class.php
Implements EntityReference_BehaviorHandler_Abstract::Delete()
OgBehaviorHandler::load in plugins/entityreference/behavior/OgBehaviorHandler.class.php
Implements EntityReference_BehaviorHandler_Abstract::load().
OgBehaviorHandlerTestCase::testGroupAudienceField in ./og.test
Test piping group association via the group-audience field.
OgBehaviorHandlerTestCase::testGroupAudienceFieldSkipBehavior in ./og.test
Test skipping OgBehaviorHandler.
OgBehaviorHandlerTestCase::testSetStateOnInsert in ./og.test
Test settings the OG membership state via field values, when associating a new group-content to a group.

... See full list

1 string reference to 'og_get_entity_groups'
og_invalidate_cache in ./og.module
Invalidate cache.

File

./og.module, line 2324
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,
), $field_name = NULL) {
  $cache =& drupal_static(__FUNCTION__, array());
  if ($entity_type == 'user' && empty($entity)) {
    global $user;
    $entity = clone $user;
  }
  if (is_object($entity)) {

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

  // Get a string identifier of the states, so we can retrieve it from cache.
  if ($states) {
    sort($states);
    $state_identifier = implode(':', $states);
  }
  else {
    $state_identifier = FALSE;
  }
  $identifier = array(
    $entity_type,
    $id,
    $state_identifier,
    $field_name,
  );
  $identifier = implode(':', $identifier);
  if (isset($cache[$identifier])) {

    // Return cached values.
    return $cache[$identifier];
  }
  $cache[$identifier] = array();
  $query = db_select('og_membership', 'ogm')
    ->fields('ogm', array(
    'id',
    'gid',
    'group_type',
  ))
    ->condition('entity_type', $entity_type)
    ->condition('etid', $id);
  if ($states) {
    $query
      ->condition('state', $states, 'IN');
  }
  if ($field_name) {
    $query
      ->condition('field_name', $field_name);
  }
  $result = $query
    ->execute()
    ->fetchAll();
  foreach ($result as $row) {
    $cache[$identifier][$row->group_type][$row->id] = $row->gid;
  }
  return $cache[$identifier];
}