You are here

public function Group::addEntity in Group 7

Add an entity to a group.

You could also manipulate the 'group' property on the entity you wish to add. Upon saving that entity, it will automatically be added to the group.

Parameters

int $entity_id: The id of the entity.

string $entity_type: The type of the entity.

string $bundle: The bundle of the entity.

1 call to Group::addEntity()
Group::addSubgroup in classes/group.inc
Add a subgroup to a group.

File

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

Class

Group
Main class for groups.

Code

public function addEntity($entity_id, $entity_type, $bundle) {
  $entity_info = entity_get_info($entity_type);

  // Can't add entities that aren't group entities.
  if (empty($entity_info['group entity'])) {
    return;
  }
  $query = db_insert('group_entity');
  $query
    ->fields(array(
    'gid' => $this->gid,
    'entity_id' => $entity_id,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
  ));
  $query
    ->execute();

  // Remove the entity from its controller's internal cache. This way it will
  // have to be loaded again next time it's requested and will thus receive
  // an updated 'group' property.
  entity_get_controller($entity_type)
    ->resetCache(array(
    $entity_id,
  ));

  // Reset the internal cache.
  $this
    ->resetCache();
}