class MediaPdfThumbnailImageManager in Media PDF Thumbnail 8
Same name and namespace in other branches
- 8.4 src/Manager/MediaPdfThumbnailImageManager.php \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager
- 8.2 src/Manager/MediaPdfThumbnailImageManager.php \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager
- 8.3 src/Manager/MediaPdfThumbnailImageManager.php \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager
Class MediaPdfThumbnailImageManager.
@package Drupal\media_pdf_thumbnail\Manager
Hierarchy
- class \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImageManager
Expanded class hierarchy of MediaPdfThumbnailImageManager
1 string reference to 'MediaPdfThumbnailImageManager'
1 service uses MediaPdfThumbnailImageManager
File
- src/
Manager/ MediaPdfThumbnailImageManager.php, line 14
Namespace
Drupal\media_pdf_thumbnail\ManagerView source
class MediaPdfThumbnailImageManager {
/**
* MediaPdfThumbnailManager.
*
* @var \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImagickManager
*/
protected $mediaPdfThumbnailImagickManager;
/**
* EntityTypeManager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* FileSystem.
*
* @var \Drupal\Core\File\FileSystem
*/
protected $fileSystem;
/**
* MediaPdfThumbnailManager constructor.
*
* @param \Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailImagickManager $mediaPdfThumbnailImagickManager
* $mediaPdfThumbnailImagickManager.
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
* EntityTypeManager.
* @param \Drupal\Core\File\FileSystem $fileSystem
* FileSystem.
*/
public function __construct(MediaPdfThumbnailImagickManager $mediaPdfThumbnailImagickManager, EntityTypeManager $entityTypeManager, FileSystem $fileSystem) {
$this->mediaPdfThumbnailImagickManager = $mediaPdfThumbnailImagickManager;
$this->entityTypeManager = $entityTypeManager;
$this->fileSystem = $fileSystem;
}
/**
* Create pdf thumbnail.
*
* @param \Drupal\Core\Entity\Entity $entity
* Entity.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
* @throws \ImagickException
*/
public function createThumbnail(Entity $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') {
$fileUri = $fileEntity
->getFileUri();
$fileUriArray = explode('/', $fileUri);
$fileName = end($fileUriArray);
$sourcePath = $this->fileSystem
->realpath($fileUri);
if ($sourcePath) {
$imageFileUri = $this->mediaPdfThumbnailImagickManager
->generateImage($sourcePath, $fileName . '.png');
$fileEntityId = $this
->createThumbnailFileEntity($imageFileUri);
if ($fileEntityId) {
$entity
->set('thumbnail', [
'target_id' => $fileEntityId,
]);
}
}
}
}
}
}
}
}
}
}
/**
* Create file entity.
*
* @param string $fileUri
* File uri.
*
* @return int|null|string
* File entity id.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\Core\Entity\EntityStorageException
*/
protected function createThumbnailFileEntity($fileUri) {
if ($fileUri) {
$fileEntity = $this->entityTypeManager
->getStorage('file')
->create([
'uri' => $fileUri,
'status' => FILE_STATUS_PERMANENT,
]);
$fileEntity
->save();
return $fileEntity
->id();
}
return NULL;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MediaPdfThumbnailImageManager:: |
protected | property | EntityTypeManager. | |
MediaPdfThumbnailImageManager:: |
protected | property | FileSystem. | |
MediaPdfThumbnailImageManager:: |
protected | property | MediaPdfThumbnailManager. | |
MediaPdfThumbnailImageManager:: |
public | function | Create pdf thumbnail. | |
MediaPdfThumbnailImageManager:: |
protected | function | Create file entity. | |
MediaPdfThumbnailImageManager:: |
public | function | MediaPdfThumbnailManager constructor. |