You are here

public function MediaPdfThumbnailService::previousFileDelete in Media PDF Thumbnail 8.4

Parameters

\Drupal\media\MediaInterface $media:

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

src/Service/MediaPdfThumbnailService.php, line 83

Class

MediaPdfThumbnailService
Class MediaPdfThumbnailService

Namespace

Drupal\media_pdf_thumbnail\Service

Code

public function previousFileDelete(MediaInterface $media) {
  $new = $media
    ->isNew();
  $newRev = $media
    ->isNewRevision();
  $infos = $this
    ->getMediaBundleSettingsInfos($media);
  $fieldHasChanged = !empty($media->original) && !$media
    ->get($infos['field'])
    ->equals($media->original
    ->get($infos['field']));

  // Simple update with no new revision.
  if (!$new && !$newRev && $fieldHasChanged) {
    $thumbnailValue = $media->original
      ->get('thumbnail')
      ->getEntity()
      ->get('thumbnail')
      ->getValue();
    $fid = !empty($thumbnailValue[0]['target_id']) ? $thumbnailValue[0]['target_id'] : NULL;
    $this->entityTypeManager
      ->getStorage('file')
      ->load($fid)
      ->delete();
  }
}