You are here

public static function FieldConfig::postDelete in Drupal 8

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

Acts on deleted entities before the delete hook is invoked.

Used after the entities are deleted but 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 EntityBase::postDelete

File

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

Class

FieldConfig
Defines the Field entity.

Namespace

Drupal\field\Entity

Code

public static function postDelete(EntityStorageInterface $storage, array $fields) {

  // Clear the cache upfront, to refresh the results of getBundles().
  \Drupal::service('entity_field.manager')
    ->clearCachedFieldDefinitions();

  // Notify the entity storage.
  foreach ($fields as $field) {
    if (!$field->deleted) {
      \Drupal::service('field_definition.listener')
        ->onFieldDefinitionDelete($field);
    }
  }

  // If this is part of a configuration synchronization then the following
  // configuration updates are not necessary.
  $entity = reset($fields);
  if ($entity
    ->isSyncing()) {
    return;
  }

  // Delete the associated field storages if they are not used anymore and are
  // not persistent.
  $storages_to_delete = [];
  foreach ($fields as $field) {
    $storage_definition = $field
      ->getFieldStorageDefinition();
    if (!$field->deleted && !$field
      ->isUninstalling() && $storage_definition
      ->isDeletable()) {

      // Key by field UUID to avoid deleting the same storage twice.
      $storages_to_delete[$storage_definition
        ->uuid()] = $storage_definition;
    }
  }
  if ($storages_to_delete) {
    \Drupal::entityTypeManager()
      ->getStorage('field_storage_config')
      ->delete($storages_to_delete);
  }
}