You are here

function field_collection_field_delete in Field collection 7

Implements hook_field_delete().

File

./field_collection.module, line 686
Module implementing field collection field type.

Code

function field_collection_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $ids = field_collection_field_item_to_ids($items);

  // Also delete all embedded entities.
  if ($ids && field_info_field($field['field_name'])) {

    // We filter out entities that are still being referenced by other
    // host-entities. This should never be the case, but it might happened e.g.
    // when modules cloned a node without knowing about field-collection.
    $entity_info = entity_get_info($entity_type);
    $entity_id_name = $entity_info['entity keys']['id'];
    $field_column = key($field['columns']);
    foreach ($ids as $id_key => $id) {
      $query = new EntityFieldQuery();
      $entities = $query
        ->fieldCondition($field['field_name'], $field_column, $id)
        ->execute();
      unset($entities[$entity_type][$entity->{$entity_id_name}]);
      if (!empty($entities[$entity_type])) {

        // Filter this $id out.
        unset($ids[$id_key]);
      }

      // Set a flag to remember that the host entity is being deleted. See
      // FieldCollectionItemEntity::deleteHostEntityReference().
      // Doing this on $entity is not sufficient because the cache for $entity
      // may have been reset since it was loaded.  That would cause
      // hostEntity() to load it from the database later without the flag.
      $field_collection_item = field_collection_item_load($id);
      if ($field_collection_item) {
        $hostEntity = $field_collection_item
          ->hostEntity();
        if (!empty($hostEntity)) {
          $hostEntity->field_collection_deleting = TRUE;
        }
      }
      if (module_exists('pathauto')) {
        pathauto_path_delete_all('field-collection/' . str_replace('_', '-', $field['field_name']) . '/' . $id);
      }
    }
    entity_delete_multiple('field_collection_item', $ids);
  }
}