/Drupal 8

public function DataReferenceTargetDefinition::getConstraints

public DataReferenceTargetDefinition::getConstraints()

Returns an array of validation constraints.

The validation constraints of a definition consist of any for it defined constraints and default constraints, which are generated based on the definition and its data type. See \Drupal\Core\TypedData\TypedDataManager::getDefaultConstraints().

Constraints are defined via an array, having constraint plugin IDs as key and constraint options as values, e.g.

$constraints = array(
  'Range' => array('min' => 5, 'max' => 10),
  'NotBlank' => array(),

Options have to be specified using another array if the constraint has more than one or zero options. If it has exactly one option, the value should be specified without nesting it into another array:

$constraints = array(
  'EntityType' => 'node',
  'Bundle' => 'article',

Note that the specified constraints must be compatible with the data type, e.g. for data of type 'entity' the 'EntityType' and 'Bundle' constraints may be specified.

Return value

array[] An array of validation constraint definitions, keyed by constraint name. Each constraint definition can be used for instantiating \Symfony\Component\Validator\Constraint objects.

Overrides DataDefinition::getConstraints

See also




core/lib/Drupal/Core/TypedData/DataReferenceTargetDefinition.php, line 22


A typed data definition class for the entity reference target_id property.




public function getConstraints() {
  $constraints = parent::getConstraints();
  // If this data definition is marked as required for the sake of schema
  // definitions, we don't enforce it using the NotNull constraint. Instead
  // \Drupal\Core\Field\EntityReferenceItem is validated by the
  // 'ValidReference' constraint that operates at the field-item level. This
  // constraint takes into consideration that the target_id property can
  // be derived from the entity property.
  return $constraints;

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.