function StringItem::getConstraints

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php \Drupal\Core\Field\Plugin\Field\FieldType\StringItem::getConstraints()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/StringItem.php \Drupal\Core\Field\Plugin\Field\FieldType\StringItem::getConstraints()
  3. 10 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 58

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();
        $options['max'] = $max_length;
        if ($label = $this->getFieldDefinition()
            ->getLabel()) {
            $options['maxMessage'] = $this->t('%name: may not be longer than @max characters.', [
                '%name' => $label,
                '@max' => $max_length,
            ]);
        }
        $constraints[] = $constraint_manager->create('ComplexData', [
            'value' => [
                'Length' => $options,
            ],
        ]);
    }
    return $constraints;
}

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