EntityHasFieldConstraint.php 992 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Drupal\Core\Entity\Plugin\Validation\Constraint;
  3. use Symfony\Component\Validator\Constraint;
  4. /**
  5. * Checks if a value is an entity that has a specific field.
  6. *
  7. * @Constraint(
  8. * id = "EntityHasField",
  9. * label = @Translation("Entity has field", context = "Validation"),
  10. * type = { "entity" },
  11. * )
  12. */
  13. class EntityHasFieldConstraint extends Constraint {
  14. /**
  15. * The default violation message.
  16. *
  17. * @var string
  18. */
  19. public $message = 'The entity must have the %field_name field.';
  20. /**
  21. * The violation message for non-fieldable entities.
  22. *
  23. * @var string
  24. */
  25. public $notFieldableMessage = 'The entity does not support fields.';
  26. /**
  27. * The field name option.
  28. *
  29. * @var string
  30. */
  31. public $field_name;
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getDefaultOption() {
  36. return 'field_name';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getRequiredOptions() {
  42. return (array) $this->getDefaultOption();
  43. }
  44. }