ParentEntityDeleteUpdater.php in Entity Reference Hierarchy 3.x
File
src/Storage/ParentEntityDeleteUpdater.php
View source
<?php
namespace Drupal\entity_hierarchy\Storage;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\entity_hierarchy\Information\ParentCandidateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ParentEntityDeleteUpdater extends ParentEntityReactionBase {
protected $treeNodeMapper;
public function __construct(NestedSetStorageFactory $nestedSetStorageFactory, NestedSetNodeKeyFactory $nodeKeyFactory, ParentCandidateInterface $parentCandidate, EntityTreeNodeMapperInterface $treeNodeMapper) {
parent::__construct($nestedSetStorageFactory, $nodeKeyFactory, $parentCandidate);
$this->treeNodeMapper = $treeNodeMapper;
}
public static function create(ContainerInterface $container) {
return (new static($container
->get('entity_hierarchy.nested_set_storage_factory'), $container
->get('entity_hierarchy.nested_set_node_factory'), $container
->get('entity_hierarchy.information.parent_candidate'), $container
->get('entity_hierarchy.entity_tree_node_mapper')))
->setLockBackend($container
->get('lock'));
}
public function moveChildren(ContentEntityInterface $parent) {
if (!$parent
->isDefaultRevision()) {
return;
}
if (!($fields = $this->parentCandidate
->getCandidateFields($parent))) {
return;
}
$stubNode = $this->nodeKeyFactory
->fromEntity($parent);
foreach ($fields as $field_name) {
$storage = $this->nestedSetStorageFactory
->get($field_name, $parent
->getEntityTypeId());
if ($children = $storage
->findChildren($stubNode)) {
$parentNode = $storage
->findParent($stubNode);
$childEntities = $this->treeNodeMapper
->loadEntitiesForTreeNodesWithoutAccessChecks($parent
->getEntityTypeId(), $children);
foreach ($childEntities as $child_node) {
if (!$childEntities
->offsetExists($child_node)) {
continue;
}
$child_entity = $childEntities
->offsetGet($child_node);
$child_entity->{$field_name}->target_id = $parentNode ? $parentNode
->getId() : NULL;
if ($child_entity
->getEntityType()
->hasKey('revision')) {
$child_entity
->setNewRevision(FALSE);
}
$child_entity
->save();
}
}
$this
->lockTree($field_name, $parent
->getEntityTypeId());
if ($existingNode = $storage
->getNode($stubNode)) {
$storage
->deleteNode($existingNode);
}
$this
->releaseLock($field_name, $parent
->getEntityTypeId());
}
}
}