I am working on a module that adds it's needed fields automatically when you enable it's function in the UI for specific entity types. It also deletes it's needed fields when you disable it's function for an entity type. (I'm not talking about when you enable the module)
We are doing this with the following on a checkbox form that displays the entity types that can have the function:
foreach ($values['product_variations'] as $key => $value) {
foreach ($neededFields as $neededField => $data) {
$field_storage = FieldStorageConfig::loadByName('commerce_product_variation', $neededField);
if (!$field_storage) {
FieldStorageConfig::create([
'entity_type' => 'commerce_product_variation',
'field_name' => $neededField,
'type' => $data['field_type'],
'settings' => $data['settings'],
'cardinality' => $data['cardinality'],
])->save();
}
$field = FieldConfig::loadByName('commerce_product_variation', $key, $neededField);
if (!$value) {
if ($field) {
$field->delete();
}
continue;
}
This works fine, except that (from what I understand) Fields API automatically deletes the FieldStorageConfig when the last instance of a field is deleted. This would not be a big deal except when the FieldStorageConfig is deleted programmatically, any view that has these enabled fields will be deleted along with it. Whereas if you delete the fields manually though the UI, the view will just be disabled as this was added in this issue.
Even if I were to address this behavior when deleting programmatically, I would still like to display the message when there are changes to the configuration as we are still removing field instances which can result in the loss of data.
Are you sure you want to delete the field?
This action cannot be undone.
Configuration Updates
The listed configuration will be updated:
Is there an EventSubscriber to display this message, or would I need to copy the source code for this and add it to my module?
persist_with_no_fields
. If you set this to TRUE you can install the field storage as yaml file from the module's config/install folder.