crm_core-8.x-3.x-dev/modules/crm_core_user_sync/src/Plugin/Validation/Constraint/UniqueReferenceConstraintValidator.php
modules/crm_core_user_sync/src/Plugin/Validation/Constraint/UniqueReferenceConstraintValidator.php
<?php
namespace Drupal\crm_core_user_sync\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates that a field is unique for the given entity type.
*/
class UniqueReferenceConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
if (!$item = $items->first()) {
return;
}
$field_name = $items->getFieldDefinition()->getName();
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $items->getEntity();
$entity_type_id = $entity->getEntityTypeId();
$id_key = $entity->getEntityType()->getKey('id');
$id = (int) $items->getEntity()->id();
$target_id = $item->target_id;
$value_taken = (bool) \Drupal::entityQuery($entity_type_id)
// The id could be NULL, so we cast it to 0 in that case.
->condition($id_key, $id, '<>')
->condition($field_name, $target_id)
->accessCheck(FALSE)
->range(0, 1)
->count()
->execute();
if ($value_taken) {
$this->context->addViolation($constraint->message, [
'@id' => $target_id,
'@entity_type' => $entity->getEntityType()->getSingularLabel(),
'@field_name' => mb_strtolower($items->getFieldDefinition()->getLabel()),
]);
}
}
}
