You are here

function node_gallery_api_entity_delete in Node Gallery 7

Implements hook_entity_delete().

File

./node_gallery_api.module, line 573
Node Gallery module.

Code

function node_gallery_api_entity_delete($entity, $type) {
  if ($type == 'node_gallery_relationship_type') {
    field_delete_field(node_gallery_api_get_item_field_name(NULL, NULL, $entity->id));
    $gallery_nids = array();

    // Delete all relationships of this type;
    $result = db_select('node_gallery_relationship', 'ngr')
      ->fields('ngr', array(
      'id',
      'ngid',
    ))
      ->condition('relationship_type', $entity->id)
      ->execute();
    foreach ($result as $r) {
      $gallery_nids[$r->ngid] = $r->ngid;
      entity_delete('node_gallery_relationship', $r->id);
    }

    // Delete gallery information for galleries of this type.
    foreach ($gallery_nids as $ngid) {
      db_delete('node_gallery_galleries')
        ->condition('ngid', $ngid)
        ->execute();
    }
  }
  if ($type == 'og_membership' && $entity->entity_type == 'node') {
    if (node_gallery_api_is_gallery($entity->etid)) {
      $bundle_type = db_select('node', 'n')
        ->fields('n', array(
        'type',
      ))
        ->condition('n.nid', $entity->etid)
        ->execute()
        ->fetchField();
      $relationship_type = node_gallery_api_get_relationship_type($bundle_type);
      if ($relationship_type->settings['og']['sync_items']) {
        $item_nids = node_gallery_api_get_item_nids($entity->etid, TRUE, FALSE);
        if (!empty($item_nids)) {
          $og_memberships = db_select('og_membership', 'ogm')
            ->fields('ogm', array(
            'id',
          ))
            ->condition('group_type', $entity->group_type)
            ->condition('gid', $entity->gid)
            ->condition('entity_type', 'node')
            ->condition('etid', $item_nids, 'IN')
            ->execute()
            ->fetchCol();
          og_membership_delete_multiple($og_memberships);
          foreach ($item_nids as $nid) {
            cache_clear_all('field:node:' . $nid, 'cache_field');
          }
        }
      }
    }
  }
}