4

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 '18 at 11:00
10

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.

1
  • 9
    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 '19 at 15:35
2

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.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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