DataReferenceTargetDefinition.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. <?php
  2. namespace Drupal\Core\TypedData;
  3. /**
  4. * A typed data definition class for the entity reference target_id property.
  5. *
  6. * The target_id property differs from other data definitions in that it is
  7. * required at the storage level, but not at the validation level. This is
  8. * because its value can be set just-in-time using the preSave() method.
  9. *
  10. * Validation for the target_id property is provided by the 'ValidReference'
  11. * validation constraint.
  12. *
  13. * @see \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::preSave()
  14. */
  15. class DataReferenceTargetDefinition extends DataDefinition {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function getConstraints() {
  20. $constraints = parent::getConstraints();
  21. // If this data definition is marked as required for the sake of schema
  22. // definitions, we don't enforce it using the NotNull constraint. Instead
  23. // \Drupal\Core\Field\EntityReferenceItem is validated by the
  24. // 'ValidReference' constraint that operates at the field-item level. This
  25. // constraint takes into consideration that the target_id property can
  26. // be derived from the entity property.
  27. unset($constraints['NotNull']);
  28. return $constraints;
  29. }
  30. }