function TextItem::getConstraints

Same name in other branches
  1. 9 core/modules/text/src/Plugin/Field/FieldType/TextItem.php \Drupal\text\Plugin\Field\FieldType\TextItem::getConstraints()
  2. 10 core/modules/text/src/Plugin/Field/FieldType/TextItem.php \Drupal\text\Plugin\Field\FieldType\TextItem::getConstraints()
  3. 11.x core/modules/text/src/Plugin/Field/FieldType/TextItem.php \Drupal\text\Plugin\Field\FieldType\TextItem::getConstraints()

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;
}

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