You are here

function og_subgroups_get_associated_entities in Subgroups for Organic groups 7.2

Get non-users entities that are associated with a group.

Parameters

$entity_type: The entity type.

$entity: The entity object or entity ID.

$groups_only: If only group entities should be returned. Huge performance gain and used internally by this module, but is FALSE by default for legacy reasons.

Return value

An array with the entities' entity type as the key, and array - keyed by the OG membership ID and the Entity ID as the value. If nothing found, then an empty array.

1 call to og_subgroups_get_associated_entities()
og_subgroups_children_load_multiple in ./og_subgroups.common.inc
Get the inheriented groups + current group.
1 string reference to 'og_subgroups_get_associated_entities'
_og_subgroups_clear_caches_for_group in ./og_subgroups.module
Helper function.

File

./og_subgroups.common.inc, line 141
Common functions used in og_subgroups.

Code

function og_subgroups_get_associated_entities($entity_type, $entity, $fields = array(), $groups_only = FALSE) {
  $cache =& drupal_static(__FUNCTION__, array());
  $groups_only_key = $groups_only ? 'groups' : 'all';
  if (is_object($entity)) {

    // Get the entity ID.
    list($id) = entity_extract_ids($entity_type, $entity);
  }
  else {
    $id = is_numeric($entity) ? $entity : 0;
  }
  if (isset($cache[$entity_type][$id][$groups_only_key])) {

    // Return cached values.
    return $cache[$entity_type][$id][$groups_only_key];
  }
  $cache[$entity_type][$id][$groups_only_key] = array();

  // Create base query for all content and groups only.
  $query = db_select('og_membership')
    ->condition('entity_type', 'user', '!=')
    ->condition('group_type', $entity_type, '=')
    ->condition('gid', $id, '=');
  if ($fields || $fields !== FALSE && ($fields = variable_get('og_subgroups_default_fields_' . $entity_type, array()))) {
    $query
      ->condition('field_name', $fields, 'IN');
  }

  // Groups only query.
  if ($groups_only) {

    // Get all group bundles of this entity type in an array.
    $group_all_bundles = og_get_all_group_bundle();
    $group_bundles = isset($group_all_bundles[$entity_type]) ? array_keys($group_all_bundles[$entity_type]) : array();

    // Return early if there are no group bundles in this entity type.
    if (empty($group_bundles)) {
      return $cache[$entity_type][$id][$groups_only_key];
    }

    // Gather scheme information from the entity to be used in the query.
    $entity_info = entity_get_info($entity_type);
    $entity_table = $entity_info['base table'];
    $entity_key_id = $entity_info['entity keys']['id'];
    $entity_key_bundle = $entity_info['entity keys']['bundle'];

    // Add entity bundle condition using known group bundles.
    $query
      ->join($entity_table, 'entity', "etid = entity.{$entity_key_id}");
    $query
      ->condition("entity.{$entity_key_bundle}", $group_bundles, 'IN');
  }

  // Add fields and execute. Store results to cache.
  $query
    ->fields('og_membership', array(
    'entity_type',
    'id',
    'etid',
  ));
  $result = $query
    ->execute();
  if ($result) {

    // Get the group ID from the group membership.
    foreach ($result as $og_membership) {
      $cache[$entity_type][$id][$groups_only_key][$og_membership->entity_type][$og_membership->id] = $og_membership->etid;
    }
  }
  return $cache[$entity_type][$id][$groups_only_key];
}