You are here

protected function ContentEntityStorageTrait::doSave in Multiversion 8.2

Same name and namespace in other branches
  1. 8 src/Entity/Storage/ContentEntityStorageTrait.php \Drupal\multiversion\Entity\Storage\ContentEntityStorageTrait::doSave()

@todo Revisit this logic with forward revisions in mind.

File

src/Entity/Storage/ContentEntityStorageTrait.php, line 424

Class

ContentEntityStorageTrait

Namespace

Drupal\multiversion\Entity\Storage

Code

protected function doSave($id, EntityInterface $entity) {
  if ($entity->_rev->is_stub || $this->entityType
    ->get('local') || !empty($entity->original) && $entity->original->_rev->is_stub) {
    $entity
      ->isDefaultRevision(TRUE);
  }
  else {

    // Enforce new revision if any module messed with it in a hook.
    $entity
      ->setNewRevision();

    // Decide whether or not this is the default revision.
    if (!$entity
      ->isNew()) {
      $workspace = Workspace::load($this
        ->getWorkspaceId());
      $index_factory = \Drupal::service('multiversion.entity_index.factory');

      /** @var \Drupal\multiversion\Entity\Index\RevisionTreeIndexInterface $tree */
      $tree = $index_factory
        ->get('multiversion.entity_index.rev.tree', $workspace);
      $default_rev = $tree
        ->getDefaultRevision($entity
        ->uuid());
      if (empty($default_rev) || $entity->_rev->value == $default_rev) {
        $entity
          ->isDefaultRevision(TRUE);
      }
      else {
        $entity
          ->isDefaultRevision(FALSE);
      }
    }
    else {
      $entity
        ->isDefaultRevision(TRUE);
    }
  }
  return parent::doSave($id, $entity);
}