EntityReferenceHierarchyFieldItemList.php in Entity Reference Hierarchy 8.2
File
src/Plugin/Field/FieldType/EntityReferenceHierarchyFieldItemList.php
View source
<?php
namespace Drupal\entity_hierarchy\Plugin\Field\FieldType;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\entity_hierarchy\Storage\TreeLockTrait;
class EntityReferenceHierarchyFieldItemList extends EntityReferenceFieldItemList {
use TreeLockTrait;
public function postSave($update) {
if (empty($this->list)) {
$stubNode = $this
->getNestedSetNodeFactory()
->fromEntity($this
->getEntity());
$storage = $this
->getTreeStorage();
if (($existingNode = $storage
->getNode($stubNode)) && $existingNode
->getDepth() > 0) {
$fieldDefinition = $this
->getFieldDefinition();
$fieldName = $fieldDefinition
->getName();
$entityTypeId = $fieldDefinition
->getTargetEntityTypeId();
$this
->lockTree($fieldName, $entityTypeId);
$storage
->moveSubTreeToRoot($existingNode);
$this
->releaseLock($fieldName, $entityTypeId);
}
}
return parent::postSave($update);
}
protected function getNestedSetNodeFactory() {
return \Drupal::service('entity_hierarchy.nested_set_node_factory');
}
protected function getTreeStorage() {
$fieldDefinition = $this
->getFieldDefinition();
return \Drupal::service('entity_hierarchy.nested_set_storage_factory')
->get($fieldDefinition
->getName(), $fieldDefinition
->getTargetEntityTypeId());
}
public function getConstraints() {
$constraints = parent::getConstraints();
$constraint_manager = $this
->getTypedDataManager()
->getValidationConstraintManager();
$constraints[] = $constraint_manager
->create('ValidHierarchyReference', []);
return $constraints;
}
}