You are here

public static function AvatarPreview::preDelete in Avatar Kit 8

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

src/Entity/AvatarPreview.php, line 182

Class

AvatarPreview
Defines the avatar preview entity.

Namespace

Drupal\avatars\Entity

Code

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

  /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
  $file_usage = \Drupal::service('file.usage');

  /** @var static[] $entities */
  foreach ($entities as $avatar_preview) {

    // When the last usage record is gone, the file will be made temporary.
    // After a the file has been temporary for a $few_hours. The temporary,
    // file will be deleted by file_cron(), and then its cache tags will be
    // invalidated. Instead, force the cache invalidation here.
    if ($avatar_preview
      ->getAvatar()) {
      \Drupal::service('cache_tags.invalidator')
        ->invalidateTags($avatar_preview
        ->getAvatar()
        ->getCacheTags());
      $file_usage
        ->delete($avatar_preview
        ->getAvatar(), 'avatars', $avatar_preview
        ->getEntityTypeId(), $avatar_preview
        ->id(), 0);
    }
  }
}