You are here

protected function ContentEntityStorageTrait::buildRevisionBranch in Multiversion 8

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

Builds the revision branch.

Parameters

\Drupal\Core\Entity\EntityInterface $entity:

Return value

array

1 call to ContentEntityStorageTrait::buildRevisionBranch()
ContentEntityStorageTrait::save in src/Entity/Storage/ContentEntityStorageTrait.php

File

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

Class

ContentEntityStorageTrait

Namespace

Drupal\multiversion\Entity\Storage

Code

protected function buildRevisionBranch(EntityInterface $entity) {

  // We are going to index the revision ahead of save in order to accurately
  // determine if this is going to be the default revision or not. We also run
  // this logic here outside of any transactions that the parent storage
  // handler might perform. It's important that the revision index does not
  // get rolled back during exceptions. All records are kept in order to more
  // accurately build revision trees of all universally known revisions.
  $branch = [];
  $rev = $entity->_rev->value;
  $revisions = $entity->_rev->revisions;
  list($i) = explode('-', $rev);
  $count_revisions = count($revisions);
  $parent_rev = $rev;
  if ($count_revisions > $i && $entity
    ->isNew()) {
    $i = $count_revisions + 1;
  }
  elseif (!empty($entity->is_reverting)) {
    $i = $count_revisions;
    $parent_rev = !empty($revisions[0]) ? $i . '-' . $revisions[0] : $rev;
  }

  // This is a regular local save operation and a new revision token should be
  // generated. The new_edit property will be set to FALSE during replication
  // to ensure the revision token is saved as-is.
  if ($entity->_rev->new_edit || $entity->_rev->is_stub) {

    // If this is the first revision it means that there's no parent.
    // By definition the existing revision value is the parent revision.
    $parent_rev = $i == 0 ? 0 : $parent_rev;

    // Only generate a new revision if this is not a stub entity. This will
    // ensure that stub entities remain with the default value (0) to make it
    // clear on a storage level that this is a stub and not a "real" revision.
    if (!$entity->_rev->is_stub) {
      $rev = \Drupal::service('multiversion.manager')
        ->newRevisionId($entity, $i);
    }
    list(, $hash) = explode('-', $rev);
    $entity->_rev->value = $rev;
    $entity->_rev->revisions = [
      $hash,
    ];
    $branch[$rev] = [
      $parent_rev,
    ];

    // Add the parent revision to list of known revisions. This will be useful
    // if an exception is thrown during entity save and a new attempt is made.
    if ($parent_rev != 0) {
      list(, $parent_hash) = explode('-', $parent_rev);
      $entity->_rev->revisions = [
        $hash,
        $parent_hash,
      ];
    }
  }
  else {
    for ($c = 0; $c < count($revisions); ++$c) {
      $p = $c + 1;
      $rev = $i-- . '-' . $revisions[$c];
      $parent_rev = isset($revisions[$p]) ? $i . '-' . $revisions[$p] : 0;
      $branch[$rev] = [
        $parent_rev,
      ];
    }
  }
  return $branch;
}