Change record status: 
Project: 
Introduced in branch: 
11.3.x
Introduced in version: 
11.3.9
Description: 

It is now possible that when entity field info alter hooks hook_entity_base_field_info_alter() and hook_entity_bundle_field_info_alter() are invoked, the array of entity type or bundle field definitions passed to the hook methods is not complete. This can occur when field definitions are being created, such as during installation.

In hook implementations, it is advised to confirm that the field definition exists in the array of fields before trying to alter it.

Before

  #[Hook('entity_bundle_field_info_alter')]
  public function entityBundleFieldInfoAlter(array &$fields, EntityTypeInterface $entity_type, string $bundle): void {
    if ($entity_type->id() === '{ENTITY TYPE ID}' && $bundle === '{BUNDLE ID}') {
      $fields['{FIELD NAME}']->addConstraint('UniqueField');

After

  #[Hook('entity_bundle_field_info_alter')]
  public function entityBundleFieldInfoAlter(array &$fields, EntityTypeInterface $entity_type,string $bundle): void {
    if ($entity_type->id() === '{ENTITY TYPE ID}' && $bundle === '{BUNDLE ID}') {
      if (\array_key_exists('{FIELD NAME}', $field)) {
        $fields['{FIELD NAME}']->addConstraint('UniqueField');
      }
    }
  }
Impacts: 
Module developers