You are here

public function FieldItemList::getConstraints in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/FieldItemList.php \Drupal\Core\Field\FieldItemList::getConstraints()
  2. 10 core/lib/Drupal/Core/Field/FieldItemList.php \Drupal\Core\Field\FieldItemList::getConstraints()

Gets a list of validation constraints.

Return value

array Array of constraints, each being an instance of \Symfony\Component\Validator\Constraint.

Overrides TypedData::getConstraints

1 call to FieldItemList::getConstraints()
EntityReferenceFieldItemList::getConstraints in core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php
Gets a list of validation constraints.
1 method overrides FieldItemList::getConstraints()
EntityReferenceFieldItemList::getConstraints in core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php
Gets a list of validation constraints.

File

core/lib/Drupal/Core/Field/FieldItemList.php, line 261

Class

FieldItemList
Represents an entity field; that is, a list of field item objects.

Namespace

Drupal\Core\Field

Code

public function getConstraints() {
  $constraints = parent::getConstraints();

  // Check that the number of values doesn't exceed the field cardinality. For
  // form submitted values, this can only happen with 'multiple value'
  // widgets.
  $cardinality = $this
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getCardinality();
  if ($cardinality != FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
    $constraints[] = $this
      ->getTypedDataManager()
      ->getValidationConstraintManager()
      ->create('Count', [
      'max' => $cardinality,
      'maxMessage' => t('%name: this field cannot hold more than @count values.', [
        '%name' => $this
          ->getFieldDefinition()
          ->getLabel(),
        '@count' => $cardinality,
      ]),
    ]);
  }
  return $constraints;
}