You are here

public static function File::preDelete in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/src/Entity/File.php \Drupal\file\Entity\File::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 Entity::preDelete

File

core/modules/file/src/Entity/File.php, line 198
Contains \Drupal\file\Entity\File.

Class

File
Defines the file entity class.

Namespace

Drupal\file\Entity

Code

public static function preDelete(EntityStorageInterface $storage, array $entities) {
  parent::preDelete($storage, $entities);
  foreach ($entities as $entity) {

    // Delete all remaining references to this file.
    $file_usage = \Drupal::service('file.usage')
      ->listUsage($entity);
    if (!empty($file_usage)) {
      foreach ($file_usage as $module => $usage) {
        \Drupal::service('file.usage')
          ->delete($entity, $module);
      }
    }

    // Delete the actual file. Failures due to invalid files and files that
    // were already deleted are logged to watchdog but ignored, the
    // corresponding file entity will be deleted.
    file_unmanaged_delete($entity
      ->getFileUri());
  }
}