Active
Project:
Drupal core
Version:
main
Component:
field system
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
14 Feb 2022 at 13:28 UTC
Updated:
14 Feb 2022 at 13:28 UTC
Jump to comment: Most recent
The base field is missing many properties after overriding the base field.
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.
The base field override should only override properties which are defined by the override.
Comments