You are here

public function GroupController::delete in Group 7

Delete a group.

As part of the cleanup process, we delete all child entities. This will in turn trigger group_entity_delete() and remove the deleted entities' records as group entity for this group.

We do this to keep the generic entity deletion logic in one place. This could be a big performance hit for groups with a lot of content, so we need to carefully monitor how this fares.

Overrides EntityAPIController::delete

See also

group_entity_delete()

EntityAPIController::delete()

File

classes/group.controller.inc, line 72
Defines the Entity API CRUD class for groups.

Class

GroupController
Controller for group entities.

Code

public function delete($ids, DatabaseTransaction $transaction = NULL) {
  $mids = array();
  foreach (group_load_multiple($ids) as $group) {
    foreach ($group
      ->getEntities() as $entity_type => $bundles) {

      // It is safe to assume that 'group entity' is either 'single' or
      // 'multiple' because the entities could not belong to a group if
      // 'group entity' were set to FALSE.
      $entity_info = entity_get_info($entity_type);
      $single = $entity_info['group entity'] == 'single';
      foreach ($bundles as $entities) {

        // Keep track of the entities we want to delete along with the group.
        $delete_ids = array();

        // If the entities can only belong to one group, we are safe to just
        // delete all of them along with the group.
        if ($single) {
          $delete_ids = array_keys($entities);
        }
        else {
          foreach ($entities as $entity_id => $entity) {
            if (count($entity->group) == 1) {
              $delete_ids[] = $entity_id;
            }
          }
        }
        entity_delete_multiple($entity_type, $delete_ids);
      }
    }

    // Gather the membership ids to delete.
    $memberships = group_membership_load_by_group($group->gid);
    $mids = array_merge($mids, array_keys($memberships));

    // Add Path module support.
    if (module_exists('path')) {
      $this
        ->deletePath($group);
    }

    // Add Pathauto module support.
    if (module_exists('pathauto') && module_exists('entity_token')) {
      pathauto_entity_path_delete_all('group', $group, "group/{$group->gid}");
    }
  }

  // Delete group memberships.
  group_membership_delete_multiple($mids);
  parent::delete($ids, $transaction);
}