BundleConstraint.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace Drupal\Core\Entity\Plugin\Validation\Constraint;
  3. use Symfony\Component\Validator\Constraint;
  4. /**
  5. * Checks if a value is a valid entity type.
  6. *
  7. * @Constraint(
  8. * id = "Bundle",
  9. * label = @Translation("Bundle", context = "Validation"),
  10. * type = { "entity", "entity_reference" }
  11. * )
  12. */
  13. class BundleConstraint extends Constraint {
  14. /**
  15. * The default violation message.
  16. *
  17. * @var string
  18. */
  19. public $message = 'The entity must be of bundle %bundle.';
  20. /**
  21. * The bundle option.
  22. *
  23. * @var string|array
  24. */
  25. public $bundle;
  26. /**
  27. * Gets the bundle option as array.
  28. *
  29. * @return array
  30. */
  31. public function getBundleOption() {
  32. // Support passing the bundle as string, but force it to be an array.
  33. if (!is_array($this->bundle)) {
  34. $this->bundle = [$this->bundle];
  35. }
  36. return $this->bundle;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getDefaultOption() {
  42. return 'bundle';
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getRequiredOptions() {
  48. return ['bundle'];
  49. }
  50. }