View source
<?php
namespace Drupal\media_pdf_thumbnail\Service;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\State\State;
use Drupal\media\MediaInterface;
use Drupal\media_pdf_thumbnail\Form\BundlesSettingsForm;
use Drupal\media_pdf_thumbnail\Manager\MediaPdfThumbnailQueueManager;
class MediaPdfThumbnailService {
protected $configFactory;
protected $mediaPdfThumbnailQueueManager;
protected $entityTypeManager;
protected $mediaPdfThumbnailConfig;
public function __construct(ConfigFactory $configFactory, MediaPdfThumbnailQueueManager $mediaPdfThumbnailQueueManager, EntityTypeManagerInterface $entityTypeManager) {
$this->configFactory = $configFactory;
$this->mediaPdfThumbnailQueueManager = $mediaPdfThumbnailQueueManager;
$this->entityTypeManager = $entityTypeManager;
$this->mediaPdfThumbnailConfig = $configFactory
->get(BundlesSettingsForm::SETTINGS);
}
public static function finishedCallback($success, $results, $operations) {
if ($success) {
$message = \Drupal::translation()
->formatPlural(count($results), 'One task processed.', '@count tasks processed.');
\Drupal::messenger()
->addMessage($message);
}
else {
\Drupal::messenger()
->addError(t('Finished with an error.'));
}
}
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']));
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();
}
}
protected function getMediaBundleSettingsInfos(MediaInterface $entity) {
$bundle = $entity
->bundle();
return [
'enable' => $this->mediaPdfThumbnailConfig
->get($bundle . '_enable'),
'field' => $this->mediaPdfThumbnailConfig
->get($bundle . '_field'),
];
}
public function handleEntity(MediaInterface $media, $action) {
$infos = $this
->getMediaBundleSettingsInfos($media);
$task = NULL;
$thumbnailType = $infos['enable'] ? 'pdf' : 'generic';
switch ($action) {
case 'create':
$task = $thumbnailType == 'pdf' ? 'pdf' : NULL;
break;
case 'update':
if ($thumbnailType == 'generic' && !$this
->hasDefaultThumbnail($media)) {
$task = 'generic';
}
else {
if ($thumbnailType == 'pdf') {
if (!$this
->hasDefaultThumbnail($media)) {
$task = 'pdf';
}
else {
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;
}
protected function hasDefaultThumbnail(MediaInterface $media) {
$file = $media
->get('thumbnail')->entity;
$fileUri = $file ? $file
->getFileUri() : NULL;
$genericThumbnailFileName = $media
->getSource()
->getPluginDefinition()['default_thumbnail_filename'];
$genericFileUri = $this->configFactory
->get('media.settings')
->get('icon_base_uri') . '/' . $genericThumbnailFileName;
return $fileUri == $genericFileUri;
}
public static function generateThumbnail(MediaInterface $media, $pdfFieldName, $thumbnailType, bool $byPassCron) {
$mediaPdfThumbnailQueueManager = \Drupal::service('media_pdf_thumbnail.queue.manager');
$cronEnable = !empty(\Drupal::configFactory()
->get(BundlesSettingsForm::SETTINGS)
->get('cron_enable'));
$useCron = $byPassCron ? FALSE : $cronEnable;
$mediaPdfThumbnailQueueManager
->createItem($media, $pdfFieldName, $thumbnailType, $useCron);
$mediaPdfThumbnailQueueManager
->executeQueue();
}
public function regenerateAllThumbnails() {
$mediaStorage = $this->entityTypeManager
->getStorage('media');
$operations = [];
foreach ($mediaStorage
->loadMultiple() as $media) {
$languageManager = \Drupal::service('language_manager');
$languages = $languageManager
->getLanguages();
foreach ($languages as $language) {
$translation = $media
->hasTranslation($language
->getId()) ? $media
->getTranslation($language
->getId()) : NULL;
if ($translation) {
$translation
->bundle();
$fieldName = $this->mediaPdfThumbnailConfig
->get($translation
->bundle() . '_field');
$thumbnailType = $this->mediaPdfThumbnailConfig
->get($translation
->bundle() . '_enable') ? 'pdf' : 'generic';
if (empty($fieldName) || empty($thumbnailType)) {
continue;
}
$operations[] = [
'Drupal\\media_pdf_thumbnail\\Service\\MediaPdfThumbnailService::generateThumbnail',
[
$translation,
$fieldName,
$thumbnailType,
TRUE,
],
];
}
}
}
$batch = [
'title' => t('Media PDF Thumbnail'),
'operations' => $operations,
'init_message' => t('Thumbnail creating process is starting.'),
'progress_message' => t('Processed @current out of @total. Estimated time: @estimate.'),
'error_message' => t('An error occurred during processing'),
'finished' => '\\Drupal\\media_pdf_thumbnail\\Service\\MediaPdfThumbnailService::finishedCallback',
];
batch_set($batch);
}
}