Created
September 24, 2018 23:51
-
-
Save jonathanfranks/88641b0be12f9159aea5f6cd614422bc to your computer and use it in GitHub Desktop.
D8 - Delete all instances of a field in an entity type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Drupal\field\Entity\FieldStorageConfig; | |
use Drupal\field\Entity\FieldConfig; | |
$entity_type = 'node'; | |
$bundles = entity_get_bundles('node'); | |
$fields = ['field_phone', 'field_fax']; | |
foreach ($bundles as $bundle) { | |
foreach ($fields as $field_name) { | |
$field = FieldConfig::loadByName($entity_type, $bundle, $field_name); | |
if (!empty($field)) { | |
$field->delete(); | |
} | |
} | |
} | |
foreach ($fields as $field_name) { | |
$field_storage = FieldStorageConfig::loadByName($entity_type, $field_name); | |
if (!empty($field_storage)) { | |
$field_storage->delete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment