EntityTypeConstraintValidator.php 643 B

123456789101112131415161718192021222324252627
  1. <?php
  2. namespace Drupal\Core\Entity\Plugin\Validation\Constraint;
  3. use Symfony\Component\Validator\Constraint;
  4. use Symfony\Component\Validator\ConstraintValidator;
  5. /**
  6. * Validates the EntityType constraint.
  7. */
  8. class EntityTypeConstraintValidator extends ConstraintValidator {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function validate($entity, Constraint $constraint) {
  13. if (!isset($entity)) {
  14. return;
  15. }
  16. /** @var $entity \Drupal\Core\Entity\EntityInterface */
  17. if ($entity->getEntityTypeId() != $constraint->type) {
  18. $this->context->addViolation($constraint->message, ['%type' => $constraint->type]);
  19. }
  20. }
  21. }