Problem/Motivation

The base field is missing many properties after overriding the base field.

Steps to reproduce

for example with machine_name field

public static function baseFieldDefinitions(EntityTypeInterface $entity_type): array {
...

    $fields['code'] = BaseFieldDefinition::create('string')
      ->setLabel(new TranslatableMarkup('Old label'))
      ->setSetting('max_length', 128)
      ->setRequired(TRUE)
      ->addConstraint('UniqueField', [])
      ->addPropertyConstraints('value', ['Regex' => ['pattern' => '/^[a-z0-9_]+$/']])
      ->setDisplayOptions('form', [
        'type' => 'machine_name',
        'settings' => [
          'weight' => 100,
          'source_field' => 'label',
          'disable_on_edit' => TRUE,
          'standalone' => TRUE,
        ],
      ])
      ->setDisplayConfigurable('view', TRUE);
}

// The field is on box entity yellow bundle.
$fields = Drupal::service('entity_field.manager')->getFieldDefinitions('box', 'yellow');

$code_field = $fields['code'];

// Get field config for given bundle, before using base field override.
kint($code_field->getConfig('yellow'));

// Change field label.
$code_field->getConfig('yellow')->setLabel('New label')->save();

// Get field config after field override.
kint($code_field->getConfig('yellow'));

comparing the field config before and after the override, the field is missing the following properties: constraints, display, provider.

Proposed resolution

The base field override should only override properties which are defined by the override.

Comments

Pasqualle created an issue. See original summary.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.