I have created a form that adds a subset of field types to an entity (does not support bundles). (the reason for the custom form is that end users find the Drupal standard 'Manage fields' workflow too difficult) The class extends FormBase. When the 'submitForm' function is executed, the field is saved but not added to the entity.
This is the code to save a date field (with text values filled in for explanation purposes):
$field_storage_values = [ 'field_name' => 'field_test_date', 'entity_type' => 'my_entity_id', 'type' => 'datetime', 'translatable' => TRUE, ]; $field_values = [ 'field_name' => 'field_test_date', 'entity_type' => 'my_entity_id', 'bundle' => 'my_entity_id', 'label' => 'Test Date', 'translatable' => FALSE, ]; $this->entityTypeManager->getStorage('field_storage_config')->create($field_storage_values)->save(); $field = $this->entityTypeManager->getStorage('field_config')->create($field_values); $field->save();
When the form is submitted, the field is created correctly but not added to the entity.
When I export the field configuration to YML, the following appears under dependencies:
dependencies: config: - field.storage.my_entity_id.field_test_date
Creating an identical Node field results in the following field configuration YML dependencies:
dependencies: config: - field.storage.node.field_test_date - node.type.basic_page
I suspect the field created with my custom form should also add the second configuration dependency like:
I tried adding the following before the field is saved but the 'addDependency' function is protected.