You are here

class ParentEntityRevisionUpdater in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Storage/ParentEntityRevisionUpdater.php \Drupal\entity_hierarchy\Storage\ParentEntityRevisionUpdater

Defines a class for handling a parent entity is updated to a new revision.

Hierarchy

Expanded class hierarchy of ParentEntityRevisionUpdater

1 file declares its use of ParentEntityRevisionUpdater
entity_hierarchy.module in ./entity_hierarchy.module
A module to make entities hierarchical.

File

src/Storage/ParentEntityRevisionUpdater.php, line 10

Namespace

Drupal\entity_hierarchy\Storage
View source
class ParentEntityRevisionUpdater extends ParentEntityReactionBase {

  /**
   * Move children from old revision to new revision.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $oldRevision
   *   Old revision.
   * @param \Drupal\Core\Entity\ContentEntityInterface $newRevision
   *   New revision.
   */
  public function moveChildren(ContentEntityInterface $oldRevision, ContentEntityInterface $newRevision) {
    if (!$newRevision
      ->isDefaultRevision()) {

      // We don't move children to a non-default revision.
      return;
    }
    if ($newRevision
      ->getRevisionId() == $oldRevision
      ->getRevisionId()) {

      // We don't move anything if the revision isn't changing.
      return;
    }
    if (!($fields = $this->parentCandidate
      ->getCandidateFields($newRevision))) {

      // There are no fields that could point to this entity.
      return;
    }
    $oldNodeKey = $this->nodeKeyFactory
      ->fromEntity($oldRevision);
    $newNodeKey = $this->nodeKeyFactory
      ->fromEntity($newRevision);
    foreach ($fields as $field_name) {
      $this
        ->lockTree($field_name, $newRevision
        ->getEntityTypeId());

      /** @var \Pnx\NestedSet\NestedSetInterface $storage */
      $storage = $this->nestedSetStorageFactory
        ->get($field_name, $newRevision
        ->getEntityTypeId());
      if (!($newParent = $storage
        ->getNode($newNodeKey))) {
        $newParent = $storage
          ->addRootNode($newNodeKey);
      }
      if ($storage
        ->findChildren($oldNodeKey)) {
        $storage
          ->adoptChildren($storage
          ->getNode($oldNodeKey), $newParent);
      }
      $this
        ->releaseLock($field_name, $newRevision
        ->getEntityTypeId());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ParentEntityReactionBase::$nestedSetStorageFactory protected property Nested set storage.
ParentEntityReactionBase::$nodeKeyFactory protected property Node key factory.
ParentEntityReactionBase::$parentCandidate protected property Parent candidate interface.
ParentEntityReactionBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 1
ParentEntityReactionBase::__construct public function Constructs a new ParentEntityRevisionUpdater object. 1
ParentEntityRevisionUpdater::moveChildren public function Move children from old revision to new revision.
TreeLockTrait::$lockBackend protected property Lock backend.
TreeLockTrait::getLockName protected function Gets lock name.
TreeLockTrait::lockBackend protected function Gets lock backend.
TreeLockTrait::lockTree protected function Locks tree.
TreeLockTrait::releaseLock protected function Releases lock.
TreeLockTrait::setLockBackend public function Sets lock backend.