0

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?

10
  • 2
    Field storages have the property 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.
    – 4uk4
    Dec 20, 2020 at 20:28
  • Can you set that programmatically while creating the fields? Dec 20, 2020 at 20:36
  • Yes, you can, but you don't need to. You can persist the field storage the whole time the module is installed
    – 4uk4
    Dec 20, 2020 at 20:39
  • Can you share a snippet of how? Dec 20, 2020 at 20:40
  • 2
    Yes, you can set any property you want when creating the field storage. But that's not what my comments are about. It's about how to prevent that the storage gets deleted automatically so that you don't need the code anymore.
    – 4uk4
    Dec 20, 2020 at 20:58

1 Answer 1

1

As discussed in the comments the solution, in most cases, is to add the field storage to the config/install folder of the module specifying persist_with_no_fields: true.

core/modules/block_content/config/install/field.storage.block_content.body.yml

langcode: en
status: true
dependencies:
  module:
    - block_content
    - text
id: block_content.body
field_name: body
entity_type: block_content
type: text_with_summary
settings: {  }
module: text
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: true
custom_storage: false

Then the configuration updates will be displayed when the module is uninstalled.

Sometimes the dependencies on the module disappear after configuration saves because Drupal is optimizing duplicate dependencies. Then enforce the dependency for this configuration item. See How to properly manage field types for a content type created by a module?

2
  • Edited the question. I'd still like to know how to utilize the configuration updates message when deleting the field instances programmatically. Up voting the answer though, as it is helpful! Jan 3, 2021 at 10:39
  • I've added the comments as answer because I've linked the code in your question and questions with no answers get deleted after some time.
    – 4uk4
    Jan 3, 2021 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.