ValidReferenceConstraintValidator.php in Drupal 8
File
core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/ValidReferenceConstraintValidator.php
View source
<?php
namespace Drupal\Core\Entity\Plugin\Validation\Constraint;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface;
use Drupal\Core\Entity\EntityReferenceSelection\SelectionWithAutocreateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class ValidReferenceConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
protected $selectionManager;
protected $entityTypeManager;
public function __construct(SelectionPluginManagerInterface $selection_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->selectionManager = $selection_manager;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.entity_reference_selection'), $container
->get('entity_type.manager'));
}
public function validate($value, Constraint $constraint) {
if (!isset($value)) {
return;
}
$new_entities = [];
$target_ids = [];
foreach ($value as $delta => $item) {
$target_id = $item->target_id;
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;
}
if (!empty($target_id)) {
$target_ids[$delta] = $target_id;
}
}
if (!$new_entities && !$target_ids) {
return;
}
$entity = !empty($value
->getParent()) ? $value
->getEntity() : NULL;
$handler = $this->selectionManager
->getSelectionHandler($value
->getFieldDefinition(), $entity);
$target_type_id = $value
->getFieldDefinition()
->getSetting('target_type');
if ($new_entities) {
if ($handler instanceof SelectionWithAutocreateInterface) {
$valid_new_entities = $handler
->validateReferenceableNewEntities($new_entities);
$invalid_new_entities = array_diff_key($new_entities, $valid_new_entities);
}
else {
$invalid_new_entities = $new_entities;
}
foreach ($invalid_new_entities as $delta => $entity) {
$this->context
->buildViolation($constraint->invalidAutocreateMessage)
->setParameter('%type', $target_type_id)
->setParameter('%label', $entity
->label())
->atPath((string) $delta . '.entity')
->setInvalidValue($entity)
->addViolation();
}
}
if ($target_ids) {
$previously_referenced_ids = [];
if ($value
->getParent() && ($entity = $value
->getEntity()) && !$entity
->isNew()) {
$existing_entity = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->loadUnchanged($entity
->id());
foreach ($existing_entity->{$value
->getFieldDefinition()
->getName()}
->getValue() as $item) {
$previously_referenced_ids[$item['target_id']] = $item['target_id'];
}
}
$valid_target_ids = $handler
->validateReferenceableEntities($target_ids);
if ($invalid_target_ids = array_diff($target_ids, $valid_target_ids)) {
$existing_entities = $this->entityTypeManager
->getStorage($target_type_id)
->loadMultiple($invalid_target_ids);
foreach ($invalid_target_ids as $delta => $target_id) {
if (isset($previously_referenced_ids[$target_id]) && isset($existing_entities[$target_id]) && !$existing_entities[$target_id]
->access('view')) {
continue;
}
$message = isset($existing_entities[$target_id]) ? $constraint->message : $constraint->nonExistingMessage;
$this->context
->buildViolation($message)
->setParameter('%type', $target_type_id)
->setParameter('%id', $target_id)
->atPath((string) $delta . '.target_id')
->setInvalidValue($target_id)
->addViolation();
}
}
}
}
}