You are here

function registration_entity_delete in Entity Registration 8.2

Same name and namespace in other branches
  1. 8 registration.module \registration_entity_delete()
  2. 7.2 registration.module \registration_entity_delete()
  3. 7 registration.module \registration_entity_delete()

Implements hook_entity_delete().

File

./registration.module, line 398

Code

function registration_entity_delete($entity, $type) {
  list($entity_id) = entity_extract_ids($type, $entity);
  if (!$entity_id) {
    return;
  }
  if ($type == 'user') {

    // Delete all registrations owned by the user.
    $query = new EntityFieldQuery();
    $result = $query
      ->entityCondition('entity_type', 'registration')
      ->propertyCondition('author_uid', $entity_id)
      ->execute();
    if ($result) {
      registration_delete_multiple(array_keys($result['registration']));

      // Delete entries in the cache as well.
      cache_clear_all('*', 'cache_entity_registration', TRUE);
    }
  }

  // Look for registration type values related to the entity being deleted.
  $relevant_types = array();
  foreach (registration_get_types() as $registration_type) {
    if ($registration_type->registrant_entity_type == $type) {
      $relevant_types[] = $registration_type->name;
    }
  }
  if (count($relevant_types)) {

    // Users associated with {registration}.registrant_id do not own the
    // registration. Simply disassociate the registrant.
    db_update('registration')
      ->fields(array(
      'registrant_id' => NULL,
    ))
      ->condition('type', $relevant_types)
      ->condition('registrant_id', $entity_id)
      ->execute();

    // Delete entries in the cache as well.
    cache_clear_all('*', 'cache_entity_registration', TRUE);
  }

  // Delete registrations and settings for this host entity .
  db_delete('registration')
    ->condition('entity_id', $entity_id)
    ->condition('entity_type', $type)
    ->execute();
  db_delete('registration_entity')
    ->condition('entity_id', $entity_id)
    ->condition('entity_type', $type)
    ->execute();
}