function FieldItemList::getConstraints

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

Overrides TypedData::getConstraints

1 call to FieldItemList::getConstraints()
EntityReferenceFieldItemList::getConstraints in core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php
1 method overrides FieldItemList::getConstraints()
EntityReferenceFieldItemList::getConstraints in core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php

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) {
        $options['max'] = $cardinality;
        if ($label = $this->getFieldDefinition()
            ->getLabel()) {
            $options['maxMessage'] = $this->t('%name: this field cannot hold more than @count values.', [
                '%name' => $label,
                '@count' => $cardinality,
            ]);
        }
        $constraints[] = $this->getTypedDataManager()
            ->getValidationConstraintManager()
            ->create('Count', $options);
    }
    return $constraints;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.