class ValidHierarchyReferenceConstraintValidator in Entity Reference Hierarchy 8.2
Same name and namespace in other branches
- 3.x src/Plugin/Validation/Constraint/ValidHierarchyReferenceConstraintValidator.php \Drupal\entity_hierarchy\Plugin\Validation\Constraint\ValidHierarchyReferenceConstraintValidator
Checks if referenced entities are valid.
Hierarchy
- class \Drupal\entity_hierarchy\Plugin\Validation\Constraint\ValidHierarchyReferenceConstraintValidator extends \Symfony\Component\Validator\ConstraintValidator implements ContainerInjectionInterface
Expanded class hierarchy of ValidHierarchyReferenceConstraintValidator
File
- src/
Plugin/ Validation/ Constraint/ ValidHierarchyReferenceConstraintValidator.php, line 17
Namespace
Drupal\entity_hierarchy\Plugin\Validation\ConstraintView source
class ValidHierarchyReferenceConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Node storage factory.
*
* @var \Drupal\entity_hierarchy\Storage\NestedSetStorageFactory
*/
protected $nestedSetStorageFactory;
/**
* Node key factory.
*
* @var \Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory
*/
protected $keyFactory;
/**
* Constructs a ValidReferenceConstraintValidator object.
*
* @param \Drupal\entity_hierarchy\Storage\NestedSetStorageFactory $nestedSetStorageFactory
* Nested set factory.
* @param \Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory $keyFactory
* Key factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(NestedSetStorageFactory $nestedSetStorageFactory, NestedSetNodeKeyFactory $keyFactory, EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->nestedSetStorageFactory = $nestedSetStorageFactory;
$this->keyFactory = $keyFactory;
}
/**
* {@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_type.manager'));
}
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
/** @var \Drupal\Core\Field\FieldItemListInterface $value */
/** @var ValidHierarchyReferenceConstraint $constraint */
if (!isset($value)) {
return;
}
// Collect new entities and IDs of existing entities across the field items.
$new_entities = [];
$target_ids = [];
if (!$value
->getEntity()) {
// Can't validate if we don't have access to host entity.
return;
}
foreach ($value as $delta => $item) {
$target_id = $item->target_id;
// We don't use a regular NotNull constraint for the target_id property as
// NULL is allowed if the entity property contains an unsaved entity.
// @see \Drupal\Core\TypedData\DataReferenceTargetDefinition::getConstraints()
if (!$item
->isEmpty() && $target_id === NULL) {
if (!$item->entity
->isNew()) {
$this->context
->buildViolation($constraint->nullMessage)
->atPath((string) $delta)
->addViolation();
return;
}
$new_entities[$delta] = $item->entity;
}
// '0' or NULL are considered valid empty references.
if (!empty($target_id)) {
$target_ids[$delta] = $target_id;
}
}
// Early opt-out if nothing to validate.
if (!$new_entities && !$target_ids) {
return;
}
$this_entity = $value
->getEntity();
$thisNode = $this->keyFactory
->fromEntity($this_entity);
$target_type = $this_entity
->getEntityTypeId();
/** @var \PNX\NestedSet\Storage\DbalNestedSet $storage */
$storage = $this->nestedSetStorageFactory
->get($value
->getFieldDefinition()
->getFieldStorageDefinition()
->getName(), $target_type);
$children = array_map(function (Node $node) {
return $node
->getId();
}, $storage
->findDescendants($thisNode));
// Cannot reference self either.
$children[] = $this_entity
->id();
// Add violations on deltas with a target_id that is not valid.
if ($target_ids && $children) {
$deltas = array_flip($target_ids);
foreach ($children as $entity_id) {
if (isset($deltas[$entity_id])) {
$this->context
->buildViolation($constraint->message)
->setParameter('%type', $target_type)
->setParameter('%id', $entity_id)
->atPath((string) $deltas[$entity_id] . '.target_id')
->setInvalidValue($entity_id)
->addViolation();
}
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ValidHierarchyReferenceConstraintValidator:: |
protected | property | The entity type manager. | |
ValidHierarchyReferenceConstraintValidator:: |
protected | property | Node key factory. | |
ValidHierarchyReferenceConstraintValidator:: |
protected | property | Node storage factory. | |
ValidHierarchyReferenceConstraintValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
ValidHierarchyReferenceConstraintValidator:: |
public | function | Checks if the passed value is valid. | |
ValidHierarchyReferenceConstraintValidator:: |
public | function | Constructs a ValidReferenceConstraintValidator object. |