EntityHasFieldConstraintValidator.php in Drupal 10
File
core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityHasFieldConstraintValidator.php
View source
<?php
namespace Drupal\Core\Entity\Plugin\Validation\Constraint;
use Drupal\Core\Entity\FieldableEntityInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class EntityHasFieldConstraintValidator extends ConstraintValidator {
public function validate($entity, Constraint $constraint) {
if (!isset($entity)) {
return;
}
if (!$entity instanceof FieldableEntityInterface) {
$this->context
->addViolation($constraint->notFieldableMessage);
return;
}
if (!$entity
->hasField($constraint->field_name)) {
$this->context
->addViolation($constraint->message, [
'%field_name' => $constraint->field_name,
]);
}
}
}