You are here

public function MediaPdfThumbnailImageManager::createThumbnail in Media PDF Thumbnail 8.2

Same name and namespace in other branches
  1. 8.4 src/Manager/MediaPdfThumbnailImageManager.php \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager::createThumbnail()
  2. 8 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()

Create pdf thumbnail.

Parameters

$entity:

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

File

src/Manager/MediaPdfThumbnailImageManager.php, line 62

Class

MediaPdfThumbnailImageManager
Class MediaPdfThumbnailImageManager.

Namespace

Drupal\media_pdf_thumbnail\Manager

Code

public function createThumbnail($entity) {
  $fieldDefinitions = $entity
    ->getFieldDefinitions();
  if ($fieldDefinitions) {
    foreach ($fieldDefinitions as $fieldDefinition) {
      if ($fieldDefinition
        ->getType() == "file") {
        if (!empty($entity
          ->get($fieldDefinition
          ->getName())
          ->getValue())) {
          foreach ($entity
            ->get($fieldDefinition
            ->getName())
            ->getValue() as $fieldValue) {
            if (array_key_exists('target_id', $fieldValue)) {
              $tid = $fieldValue['target_id'];
              $fileEntity = $this->entityTypeManager
                ->getStorage('file')
                ->load($tid);
              if ($fileEntity
                ->getMimeType() == 'application/pdf') {
                $fileInfos = $this
                  ->getFileInfos($fileEntity);
                $isCreated = isset($fileInfos['source']) && isset($fileInfos['destination']) ? $this->mediaPdfThumbnailImagickManager
                  ->generateImage($fileInfos['source'], $fileInfos['destination']) : '';
                $fileEntityId = !$isCreated ?: $this
                  ->createThumbnailFileEntity($fileEntity
                  ->getFileUri());
                if ($fileEntityId) {
                  $entity
                    ->set('thumbnail', [
                    'target_id' => $fileEntityId,
                  ]);
                }
              }
            }
          }
        }
      }
    }
  }
}