field_entity_bundle_delete($entity_type_id, $bundle)
Implements hook_entity_bundle_delete().
function field_entity_bundle_delete($entity_type_id, $bundle) { $storage = \Drupal::entityManager()->getStorage('field_config'); // Get the fields on the bundle. $fields = $storage->loadByProperties(['entity_type' => $entity_type_id, 'bundle' => $bundle]); // This deletes the data for the field as well as the field themselves. This // function actually just marks the data and fields as deleted, leaving the // garbage collection for a separate process, because it is not always // possible to delete this much data in a single page request (particularly // since for some field types, the deletion is more than just a simple DELETE // query). foreach ($fields as $field) { $field->delete(); } // We are duplicating the work done by // \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::onDependencyRemoval() // because we need to take into account bundles that are not provided by a // config entity type so they are not part of the config dependencies. // Gather a list of all entity reference fields. $map = \Drupal::entityManager()->getFieldMapByFieldType('entity_reference'); $ids = []; foreach ($map as $type => $info) { foreach ($info as $name => $data) { foreach ($data['bundles'] as $bundle_name) { $ids[] = "$type.$bundle_name.$name"; } } } // Update the 'target_bundles' handler setting if needed. foreach (FieldConfig::loadMultiple($ids) as $field_config) { if ($field_config->getSetting('target_type') == $entity_type_id) { $handler_settings = $field_config->getSetting('handler_settings'); if (isset($handler_settings['target_bundles'][$bundle])) { unset($handler_settings['target_bundles'][$bundle]); $field_config->setSetting('handler_settings', $handler_settings); $field_config->save(); // In case we deleted the only target bundle allowed by the field we // have to log a critical message because the field will not function // correctly anymore. if ($handler_settings['target_bundles'] === []) { \Drupal::logger('entity_reference')->critical('The %target_bundle bundle (entity type: %target_entity_type) was deleted. As a result, the %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', [ '%target_bundle' => $bundle, '%target_entity_type' => $entity_type_id, '%field_name' => $field_config->getName(), '%entity_type' => $field_config->getTargetEntityTypeId(), '%bundle' => $field_config->getTargetBundle() ]); } } } } }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!field!field.module/function/field_entity_bundle_delete/8.1.x