You are here

public function MediaPdfThumbnailImageManager::createThumbnail in Media PDF Thumbnail 8.4

Same name and namespace in other branches
  1. 8 src/Manager/MediaPdfThumbnailImageManager.php \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager::createThumbnail()
  2. 8.2 src/Manager/MediaPdfThumbnailImageManager.php \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager::createThumbnail()
  3. 8.3 src/Manager/MediaPdfThumbnailImageManager.php \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager::createThumbnail()

Parameters

\Drupal\media\MediaInterface $media:

$fieldName:

$thumbnailType:

Return value

false

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

\Drupal\Core\TypedData\Exception\MissingDataException

File

src/Manager/MediaPdfThumbnailImageManager.php, line 90

Class

MediaPdfThumbnailImageManager
Class MediaPdfThumbnailImageManager.

Namespace

Drupal\media_pdf_thumbnail\Manager

Code

public function createThumbnail(MediaInterface $media, $fieldName, $thumbnailType) {
  if (empty($media)) {
    return FALSE;
  }
  $entityTypeId = $media
    ->getEntityTypeId();
  if ($entityTypeId == 'media' && $fieldName) {
    if ($media
      ->hasField($fieldName) && !empty($media
      ->get($fieldName)
      ->getValue())) {
      $fileEntity = $this
        ->getFileEntity($media
        ->get($fieldName)
        ->getValue()[0]['target_id']);
      if ($fileEntity && in_array($fileEntity
        ->getMimeType(), self::VALID_MIME_TYPE)) {

        // Default thumbnail.
        $fileEntityInfo['fid'] = $this
          ->getGenericThumbnail();
        $fileEntityInfo['filename'] = NULL;
        if ($thumbnailType == 'pdf') {
          $fileEntityInfo = $this
            ->createThumbnailFileEntity($this
            ->generatePdfImage($fileEntity));
        }
        if (!empty($fileEntityInfo)) {
          $this
            ->setImageToMediaThumbnail($media, $fileEntityInfo, $thumbnailType);
        }
      }
    }
  }
}