You are here

public static function FieldConfig::preDelete in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/src/Entity/FieldConfig.php \Drupal\field\Entity\FieldConfig::preDelete()

Acts on entities before they are deleted and before hooks are invoked.

Used before the entities are deleted and before invoking the delete hook.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entities.

Overrides ConfigEntityBase::preDelete

File

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

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);
    }
  }
}