function FieldConfig::preDelete

Same name in other branches
  1. 8.9.x core/modules/field/src/Entity/FieldConfig.php \Drupal\field\Entity\FieldConfig::preDelete()
  2. 10 core/modules/field/src/Entity/FieldConfig.php \Drupal\field\Entity\FieldConfig::preDelete()
  3. 11.x core/modules/field/src/Entity/FieldConfig.php \Drupal\field\Entity\FieldConfig::preDelete()

Overrides ConfigEntityBase::preDelete

File

core/modules/field/src/Entity/FieldConfig.php, line 198

Class

FieldConfig
Defines the Field entity.

Namespace

Drupal\field\Entity

Code

public static function preDelete(EntityStorageInterface $storage, array $fields) {
    
    /** @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository */
    $deleted_fields_repository = \Drupal::service('entity_field.deleted_fields_repository');
    $entity_type_manager = \Drupal::entityTypeManager();
    parent::preDelete($storage, $fields);
    // Keep the field definitions in the deleted fields repository so we can use
    // them later during field_purge_batch().
    
    /** @var \Drupal\field\FieldConfigInterface $field */
    foreach ($fields as $field) {
        // Only mark a field for purging if there is data. Otherwise, just remove
        // it.
        $target_entity_storage = $entity_type_manager->getStorage($field->getTargetEntityTypeId());
        if (!$field->deleted && $target_entity_storage instanceof FieldableEntityStorageInterface && $target_entity_storage->countFieldData($field->getFieldStorageDefinition(), TRUE)) {
            $field = clone $field;
            $field->deleted = TRUE;
            $field->fieldStorage = NULL;
            $deleted_fields_repository->addFieldDefinition($field);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.