TransWikia.com

How to display configuration updates when deleting fields programatically

Drupal Answers Asked by tonytheferg on January 29, 2021

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?

One Answer

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?

Answered by 4k4 on January 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP