You are here

public function LanguageItem::getConstraints in Custom Language field 8

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

src/Plugin/Field/FieldType/LanguageItem.php, line 249

Class

LanguageItem
Plugin implementation of the 'language' field type.

Namespace

Drupal\languagefield\Plugin\Field\FieldType

Code

public function getConstraints() {
  $constraints = [];

  // @Usage When adding parent::getConstraints(), only English is allowed.
  $max_length = $this
    ->getSetting('max_length');
  if ($max_length) {
    $constraint_manager = \Drupal::typedDataManager()
      ->getValidationConstraintManager();
    $constraints[] = $constraint_manager
      ->create('ComplexData', [
      'value' => [
        'Length' => [
          'max' => $max_length,
          'maxMessage' => $this
            ->t('%name: may not be longer than @max characters.', [
            '%name' => $this
              ->getFieldDefinition()
              ->getLabel(),
            '@max' => $max_length,
          ]),
        ],
      ],
    ]);
  }
  return $constraints;
}