You are here

public function TextItem::getConstraints in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/text/src/Plugin/Field/FieldType/TextItem.php \Drupal\text\Plugin\Field\FieldType\TextItem::getConstraints()
  2. 9 core/modules/text/src/Plugin/Field/FieldType/TextItem.php \Drupal\text\Plugin\Field\FieldType\TextItem::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

File

core/modules/text/src/Plugin/Field/FieldType/TextItem.php, line 55

Class

TextItem
Plugin implementation of the 'text' field type.

Namespace

Drupal\text\Plugin\Field\FieldType

Code

public function getConstraints() {
  $constraint_manager = \Drupal::typedDataManager()
    ->getValidationConstraintManager();
  $constraints = parent::getConstraints();
  if ($max_length = $this
    ->getSetting('max_length')) {
    $constraints[] = $constraint_manager
      ->create('ComplexData', [
      'value' => [
        'Length' => [
          'max' => $max_length,
          'maxMessage' => t('%name: the text may not be longer than @max characters.', [
            '%name' => $this
              ->getFieldDefinition()
              ->getLabel(),
            '@max' => $max_length,
          ]),
        ],
      ],
    ]);
  }
  return $constraints;
}