You are here

function field_test_entity_bundle_field_info_alter in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/modules/field_test/field_test.module \field_test_entity_bundle_field_info_alter()
  2. 10 core/modules/field/tests/modules/field_test/field_test.module \field_test_entity_bundle_field_info_alter()

Implements hook_entity_bundle_field_info_alter().

File

core/modules/field/tests/modules/field_test/field_test.module, line 186
Helper module for the Field API tests.

Code

function field_test_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
  if (($field_name = \Drupal::state()
    ->get('field_test_constraint', FALSE)) && $entity_type
    ->id() == 'entity_test' && $bundle == 'entity_test' && !empty($fields[$field_name])) {

    // Set a property constraint using
    // \Drupal\Core\Field\FieldConfigInterface::setPropertyConstraints().
    $fields[$field_name]
      ->setPropertyConstraints('value', [
      'TestField' => [
        'value' => -2,
        'message' => t('%name does not accept the value @value.', [
          '%name' => $field_name,
          '@value' => -2,
        ]),
      ],
    ]);

    // Add a property constraint using
    // \Drupal\Core\Field\FieldConfigInterface::addPropertyConstraints().
    $fields[$field_name]
      ->addPropertyConstraints('value', [
      'Range' => [
        'min' => 0,
        'max' => 32,
      ],
    ]);
  }
}