You are here

public function Group::getEntities in Group 7

Get all child entities.

Parameters

bool $flat: (optional) Whether or not to flatten the result array.

bool $reset: (optional) Whether to reset the internal cache.

Return value

array An array of child entities, keyed by their type, bundle and entity id. When $flat is set to TRUE, the return value is one big array of entities, which are keyed numerically and not by their entity id!

1 call to Group::getEntities()
Group::getEntitiesOfType in classes/group.inc
Get all child entities of a certain type.

File

classes/group.inc, line 133
Defines the Entity API class for groups.

Class

Group
Main class for groups.

Code

public function getEntities($flat = FALSE, $reset = FALSE) {
  if (empty($this->entityCache) || $reset) {
    $entities = array(
      'flat' => array(),
      'full' => array(),
    );
    $query = db_select('group_entity', 'ge');
    $query
      ->condition('ge.gid', $this->gid);
    $query
      ->addField('ge', 'entity_type');
    $query
      ->addField('ge', 'entity_id');
    $query
      ->addField('ge', 'bundle');

    // Divide the retrieved entity ids by type and bundle.
    foreach ($query
      ->execute() as $record) {
      $entities['full'][$record->entity_type][$record->bundle][] = $record->entity_id;
    }

    // Actually load the retrieved entities.
    foreach ($entities['full'] as $type => $bundles) {
      foreach ($bundles as $bundle => $ids) {
        $entities['full'][$type][$bundle] = entity_load($type, $ids);

        // We run the entity_load() result through array_values() to prevent
        // people from making the false assumption that we still return the
        // entity ids as array keys.
        $entities['flat'] = array_merge($entities['flat'], array_values($entities['full'][$type][$bundle]));
      }
    }
    $this->entityCache = $entities;
  }
  return $flat ? $this->entityCache['flat'] : $this->entityCache['full'];
}