You are here

function group_entity_update in Group 7

Implements hook_entity_update().

File

./group.entity.inc, line 662
Contains all Entity API functions for the Group module.

Code

function group_entity_update($entity, $type) {
  $entity_info = entity_get_info($type);
  if (!empty($entity_info['group entity'])) {

    // Retrieve the entity id and bundle.
    list($entity_id, $revision_id, $bundle) = entity_extract_ids($type, $entity);

    // Load the original entity to detect changes.
    $original = entity_load_unchanged($type, $entity_id);

    // Gather the original and updated group ids as arrays.
    $original_gids = isset($original->group) ? (array) $original->group : array();
    $updated_gids = isset($entity->group) ? (array) $entity->group : array();

    // Remove the entity from groups it no longer belongs to.
    if ($gids = array_diff($original_gids, $updated_gids)) {
      foreach (group_load_multiple($gids) as $group) {
        $group
          ->removeEntity($entity_id, $type);
      }
    }

    // Saving takes care of creating new links.
    group_entity_save($entity, $type);
  }
}