You are here

public function FieldCollectionItemEntity::deleteRevision in Field collection 7

Intelligently delete a field collection item revision.

If a host entity is revisioned with its field collection items, deleting a field collection item on the default revision of the host should not delete the collection item from archived revisions too. Instead, we delete the current default revision and archive the field collection.

File

./field_collection.entity.inc, line 583

Class

FieldCollectionItemEntity
Class for field_collection_item entities.

Code

public function deleteRevision($skip_host_update = FALSE) {
  if (!$this->revision_id) {
    return;
  }
  if (!$skip_host_update) {

    // Just remove the item from the host, which cares about deleting the
    // item (depending on whether the update creates a new revision).
    $this
      ->deleteHostEntityReference();
  }
  if (!$this
    ->isDefaultRevision()) {
    entity_revision_delete('field_collection_item', $this->revision_id);
  }
  else {
    $row = db_select('field_collection_item_revision', 'r')
      ->fields('r')
      ->condition('item_id', $this->item_id)
      ->condition('revision_id', $this->revision_id, '<>')
      ->execute()
      ->fetchAssoc();
    if ($row) {

      // Make the other revision the default revision and archive the item.
      db_update('field_collection_item')
        ->fields(array(
        'archived' => 1,
        'revision_id' => $row['revision_id'],
      ))
        ->condition('item_id', $this->item_id)
        ->execute();

      // Let other modules know about the archived item.
      entity_get_controller('field_collection_item')
        ->invoke('archive', $this);
      entity_get_controller('field_collection_item')
        ->resetCache(array(
        $this->item_id,
      ));
      entity_revision_delete('field_collection_item', $this->revision_id);
    }
    else {

      // Delete if there is no existing revision or translation to be saved.
      $this
        ->delete($skip_host_update);
    }
  }
}