public function CpfUniqueConstraintValidator::validate in CPF 8
Same name and namespace in other branches
- 8.2 src/Plugin/Validation/Constraint/CpfUniqueConstraintValidator.php \Drupal\cpf\Plugin\Validation\Constraint\CpfUniqueConstraintValidator::validate()
File
- src/
Plugin/ Validation/ Constraint/ CpfUniqueConstraintValidator.php, line 17
Class
- CpfUniqueConstraintValidator
- Validates that a field is unique for the given entity type.
Namespace
Drupal\cpf\Plugin\Validation\ConstraintCode
public function validate($value, Constraint $constraint) {
$fieldDefinition = $constraint->fieldDefinition;
$field_name = $fieldDefinition
->getName();
$entity = $constraint->entity;
$entity_type_id = $entity
->getEntityTypeId();
$id_key = $entity
->getEntityType()
->getKey('id');
$id = (int) $entity
->id();
$query = \Drupal::entityQuery($entity_type_id)
->condition($id_key, $id, '<>')
->condition($field_name, $value);
if (!$constraint->ignoreBundle) {
$query
->condition('type', $entity
->bundle());
}
$value_taken = (bool) $query
->range(0, 1)
->count()
->execute();
if ($value_taken) {
$this->context
->addViolation($constraint->message, [
'%value' => $value,
'@entity_type' => $entity
->getEntityType()
->getLowercaseLabel(),
'@field_name' => Unicode::strtolower($fieldDefinition
->getLabel()),
]);
}
}