You are here

public function MediaPdfThumbnailService::handleEntity in Media PDF Thumbnail 8.4

Parameters

\Drupal\media\MediaInterface $media:

$action:

Return value

null

Throws

\Drupal\Core\TypedData\Exception\MissingDataException

File

src/Service/MediaPdfThumbnailService.php, line 119

Class

MediaPdfThumbnailService
Class MediaPdfThumbnailService

Namespace

Drupal\media_pdf_thumbnail\Service

Code

public function handleEntity(MediaInterface $media, $action) {
  $infos = $this
    ->getMediaBundleSettingsInfos($media);
  $task = NULL;

  // Determine thumbnail type.
  $thumbnailType = $infos['enable'] ? 'pdf' : 'generic';

  // Determine task.
  switch ($action) {
    case 'create':
      $task = $thumbnailType == 'pdf' ? 'pdf' : NULL;
      break;
    case 'update':

      // If needs default thumbnail but it's not done.
      if ($thumbnailType == 'generic' && !$this
        ->hasDefaultThumbnail($media)) {
        $task = 'generic';
      }
      else {

        // If needs thumbnail from pdf.
        if ($thumbnailType == 'pdf') {

          // Default thumbnail must be changed.
          if (!$this
            ->hasDefaultThumbnail($media)) {
            $task = 'pdf';
          }
          else {

            // Thumbnail is already from pdf but the field's value has been updated. So needs regenerating a thumbnail.
            if (!$media
              ->get($infos['field'])
              ->equals($media->original
              ->get($infos['field']))) {
              $task = 'pdf';
            }
          }
        }
      }
  }
  if ($task === 'pdf') {
    self::generateThumbnail($media, $infos['field'], 'pdf', FALSE);
  }
  elseif ($task === 'generic') {
    self::generateThumbnail($media, $infos['field'], 'generic', FALSE);
  }
  return NULL;
}