function field_test_entity_bundle_field_info_alter

Same name in other branches
  1. 9 core/modules/field/tests/modules/field_test/field_test.module \field_test_entity_bundle_field_info_alter()
  2. 8.9.x 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 188

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,
            ],
        ]);
    }
}

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