You are here

public static function File::preDelete in Drupal 8

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

File

core/modules/file/src/Entity/File.php, line 196

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.
    try {
      \Drupal::service('file_system')
        ->delete($entity
        ->getFileUri());
    } catch (FileException $e) {

      // Ignore and continue.
    }
  }
}