function EntityHasFieldConstraintValidator::validate

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityHasFieldConstraintValidator.php \Drupal\Core\Entity\Plugin\Validation\Constraint\EntityHasFieldConstraintValidator::validate()
  2. 8.9.x core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityHasFieldConstraintValidator.php \Drupal\Core\Entity\Plugin\Validation\Constraint\EntityHasFieldConstraintValidator::validate()
  3. 10 core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityHasFieldConstraintValidator.php \Drupal\Core\Entity\Plugin\Validation\Constraint\EntityHasFieldConstraintValidator::validate()

File

core/lib/Drupal/Core/Entity/Plugin/Validation/Constraint/EntityHasFieldConstraintValidator.php, line 17

Class

EntityHasFieldConstraintValidator
Validates the EntityHasField constraint.

Namespace

Drupal\Core\Entity\Plugin\Validation\Constraint

Code

public function validate($entity, Constraint $constraint) : void {
    if (!isset($entity)) {
        return;
    }
    
    /** @var \Drupal\Core\Entity\Plugin\Validation\Constraint\EntityHasFieldConstraint $constraint */
    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,
        ]);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.