FieldListUniqueValuesValidator.php in Helper 8
File
src/Plugin/Validation/Constraint/FieldListUniqueValuesValidator.phpView source
<?php
namespace Drupal\helper\Plugin\Validation\Constraint;
use Drupal\helper\Field;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates duplicate field values.
*/
class FieldListUniqueValuesValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint) {
/** @var \Drupal\Core\Field\FieldItemListInterface $value */
/** @var \Drupal\helper\Plugin\Validation\Constraint\FieldListUniqueValues $constraint */
// If the field is empty or doesn't have more than one value, there is
// nothing to validate.
if (!isset($value) || count($value) <= 1) {
return;
}
if ($duplicates = Field::getDuplicateValues($value, $constraint->property)) {
if ($constraint->show_values) {
$this->context
->addViolation($constraint->messageWithValues, [
'%field_name' => $value
->getFieldDefinition()
->getLabel(),
'@values' => implode(', ', $duplicates),
]);
}
else {
$this->context
->addViolation($constraint->message, [
'%field_name' => $value
->getFieldDefinition()
->getLabel(),
]);
}
}
}
}
Classes
Name | Description |
---|---|
FieldListUniqueValuesValidator | Validates duplicate field values. |