function StringItem::getConstraints

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

Overrides TypedData::getConstraints

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php, line 52

Class

StringItem
Defines the 'string' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public function getConstraints() {
    $constraints = parent::getConstraints();
    if ($max_length = $this->getSetting('max_length')) {
        $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
        $constraints[] = $constraint_manager->create('ComplexData', [
            'value' => [
                'Length' => [
                    'max' => $max_length,
                    'maxMessage' => t('%name: 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.