9

I found this page: https://www.drupal.org/node/2012896 which lists this:

// Deleting field storage.
FieldStorageConfig::loadByName('entity_type', 'field_name')->delete();

// Deleting field.
FieldConfig::loadByName('entity_type', 'bundle', 'field_name')->delete();

but, of course, I get: Fatal error: Class 'FieldStorageConfig' not found.

Any hints as to how this gets used?

1
  • 2
    I dont understand the negative rating Jan 18, 2018 at 11:00

3 Answers 3

17

Drupal 8 is using object-oriented programming (except for some procedural bootstrapping files and the *.module and *.theme files).

When trying to access static methods of a class without a namespace, the class is expected to reside within the same namespace of the file you use the class name with.

This is not true for classes provided by Drupal core or other modules, when you use them within your own module.

So you have to properly use namespaces. When checking the API docs for example for FieldStorageConfig and scroll a little down, you will find the correct namespace of this class. The same goes for the FieldConfig class.

So you could use both classes by altering your source code to:

// Deleting field storage.
\Drupal\field\Entity\FieldStorageConfig::loadByName('entity_type', 'field_name')->delete();

// Deleting field.
\Drupal\field\Entity\FieldConfig::loadByName('entity_type', 'bundle', 'field_name')->delete();

However, it is best-practice to not prefix the class names by their full namespace, but use use directives at the top of your source code (after a possibly existing namespace declaration, if you are within the context of an own class).

This sums up to the following correct and working example:

use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;

// Deleting field storage.
FieldStorageConfig::loadByName('entity_type', 'field_name')->delete();

// Deleting field.
FieldConfig::loadByName('entity_type', 'bundle', 'field_name')->delete();

PS.: When learning Drupal 8, it is always a good idea to check how other modules/core are doing it. You can search the code base for the class in question or use the API reference, which has a search function, and you may find plenty useful examples.

3
  • 12
    Note that since the field config has a dependency on the field storage it is only needed to execute FieldStorageConfig::loadByName('entity_type', 'field_name')->delete(); and the field config will be deleted alongside it.
    – pfrenssen
    Apr 16, 2019 at 15:35
  • Following @pfrenssen's comment: you may want to invert the two instructions, so that field config is deleted before the field storage config, and you will remember what you have to do if you want to remove from a bundle but not from others. Jul 28, 2022 at 7:32
  • Does this also delete the config dependency within the node/entity form/display modes? Or does that require additional steps?
    – Chris
    Feb 8, 2023 at 17:42
3

Thank you @MarioSteinitz for your post. I just tried to enhance the snippet by check if the field exists before trying to delete it.

use Drupal\field\Entity\FieldStorageConfig;
$field = FieldStorageConfig::loadByName('entity_type', 'field_name');
if ($field) {
  // Delete this field.
  $field->delete();
}
else {
  // The field can not be loaded or maybe it does not exists.
}
2
  • How can i delete my custom table field using hook_uninstall? Apr 14, 2022 at 9:14
  • A even more tested solution maybe : if ($fieldStorage && $fieldStorage->isDeletable()) { $fieldStorage->delete(); }
    – Pauleau
    May 16, 2023 at 6:55
0

In case you want to remove the field from all bundles, without specifying bundle name:

use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;

function remove_field(string $entity_type, string $field_id): void {
  $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type);
  foreach ($bundles as $bundle) {
    if ($field_instance = FieldConfig::loadByName($entity_type, $bundle, $field_id)) {
      $field_instance->delete();
    }
  }

  if ($field_storage = FieldStorageConfig::loadByName($entity_type, $field_id)) {
    $field_storage->delete();
  }
}
1
  • 1
    There's really no nedd for that, just remove the field storage config and the field will be removed from all the bundles it is used in. Jul 28, 2022 at 7:33

Your Answer

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

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