I created a custom Entity, using the following code in the CustomEntity.php file.
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('Name'))
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'above',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
return $fields;
}
We can add fields for the entity and all bundles will automatically inherit them.
I set a bundle in the myModule/config/install/myModule.customEntity.bundleA.yml file.
uuid: db129d12-849a-4e68-bb56-d787b4b0cf42
langcode: en
status: true
dependencies: { }
id: bundleA
label: bundleA
How can I programmatically add fields to that bundle?