class ParentEntityDeleteUpdater in Entity Reference Hierarchy 3.x
Same name and namespace in other branches
- 8.2 src/Storage/ParentEntityDeleteUpdater.php \Drupal\entity_hierarchy\Storage\ParentEntityDeleteUpdater
Defines a class for updating the tree when a parent is deleted.
Hierarchy
- class \Drupal\entity_hierarchy\Storage\ParentEntityReactionBase implements ContainerInjectionInterface uses TreeLockTrait
- class \Drupal\entity_hierarchy\Storage\ParentEntityDeleteUpdater
Expanded class hierarchy of ParentEntityDeleteUpdater
1 file declares its use of ParentEntityDeleteUpdater
- entity_hierarchy.module in ./
entity_hierarchy.module - A module to make entities hierarchical.
File
- src/
Storage/ ParentEntityDeleteUpdater.php, line 12
Namespace
Drupal\entity_hierarchy\StorageView source
class ParentEntityDeleteUpdater extends ParentEntityReactionBase {
/**
* Tree node mapper.
*
* @var \Drupal\entity_hierarchy\Storage\EntityTreeNodeMapperInterface
*/
protected $treeNodeMapper;
/**
* Constructs a new ParentEntityRevisionUpdater object.
*
* @param \Drupal\entity_hierarchy\Storage\NestedSetStorageFactory $nestedSetStorageFactory
* Nested set storage factory.
* @param \Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory $nodeKeyFactory
* Node key factory.
* @param \Drupal\entity_hierarchy\Information\ParentCandidateInterface $parentCandidate
* Parent candidate service.
* @param \Drupal\entity_hierarchy\Storage\EntityTreeNodeMapperInterface $treeNodeMapper
* Tree node mapper.
*/
public function __construct(NestedSetStorageFactory $nestedSetStorageFactory, NestedSetNodeKeyFactory $nodeKeyFactory, ParentCandidateInterface $parentCandidate, EntityTreeNodeMapperInterface $treeNodeMapper) {
parent::__construct($nestedSetStorageFactory, $nodeKeyFactory, $parentCandidate);
$this->treeNodeMapper = $treeNodeMapper;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* Moves children to their grandparent or root.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $parent
* Parent being deleted.
*/
public function moveChildren(ContentEntityInterface $parent) {
if (!$parent
->isDefaultRevision()) {
// We don't do anything here.
return;
}
if (!($fields = $this->parentCandidate
->getCandidateFields($parent))) {
// There are no fields that could point to this entity.
return;
}
$stubNode = $this->nodeKeyFactory
->fromEntity($parent);
foreach ($fields as $field_name) {
/** @var \Pnx\NestedSet\NestedSetInterface $storage */
$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')) {
// We don't want a new revision here.
$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());
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ParentEntityDeleteUpdater:: |
protected | property | Tree node mapper. | |
ParentEntityDeleteUpdater:: |
public static | function |
Instantiates a new instance of this class. Overrides ParentEntityReactionBase:: |
|
ParentEntityDeleteUpdater:: |
public | function | Moves children to their grandparent or root. | |
ParentEntityDeleteUpdater:: |
public | function |
Constructs a new ParentEntityRevisionUpdater object. Overrides ParentEntityReactionBase:: |
|
ParentEntityReactionBase:: |
protected | property | Nested set storage. | |
ParentEntityReactionBase:: |
protected | property | Node key factory. | |
ParentEntityReactionBase:: |
protected | property | Parent candidate interface. | |
TreeLockTrait:: |
protected | property | Lock backend. | |
TreeLockTrait:: |
protected | function | Gets lock name. | |
TreeLockTrait:: |
protected | function | Gets lock backend. | |
TreeLockTrait:: |
protected | function | Locks tree. | |
TreeLockTrait:: |
protected | function | Releases lock. | |
TreeLockTrait:: |
public | function | Sets lock backend. |