You are here

public function ParagraphsItemEntity::deleteRevision in Paragraphs 7

Intelligently delete a paragraphs item revision.

If a host entity is revisioned with its paragraphs items, deleting a paragraphs 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 paragraph.

File

./ParagraphsItemEntity.inc, line 441

Class

ParagraphsItemEntity
Entity implementation for the paragraphs entity.

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('paragraphs_item', $this->revision_id);
  }
  else {
    $row = db_select('paragraphs_item_revision', 'r')
      ->fields('r')
      ->condition('item_id', $this->item_id)
      ->condition('revision_id', $this->revision_id, '<>')
      ->execute()
      ->fetchAssoc();

    // If no other revision is left, delete. Else archive the item.
    if (!$row) {
      $this
        ->delete();
    }
    else {

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