You are here

function ggroup_entity_delete in Group 7

Implements hook_entity_delete().

@todo Parent is deleted before children. Causes fatal EMW errors.

See also

https://drupal.org/node/1789494

File

modules/ggroup/ggroup.entity.inc, line 132
Contains all Entity API functions for the Subgroup module.

Code

function ggroup_entity_delete($entity, $type) {
  if ($type == 'group_membership') {

    // Check for any memberships that had this one as their parent.
    $children = group_memberships(array(
      'parent_mid' => $entity->mid,
    ));
    foreach ($children as $group_membership) {
      $granted = $group_membership->heritage['granted'];

      // If the inherited membership was 'clean' or had no extra roles granted,
      // we delete it along with the parent.
      if ($group_membership->status == 'inherited' || empty($granted)) {
        $group_membership
          ->delete();
      }
      else {
        $group_membership->roles = $granted;
        $group_membership->status = 'active';
        $group_membership->heritage = NULL;
        $group_membership->parent_mid = NULL;
        $group_membership
          ->save();
      }
    }
  }
}