diff --git a/src/Plugin/Action/CreatePdfDerivative.php b/src/Plugin/Action/CreatePdfDerivative.php index 9e78de2..22fe965 100644 --- a/src/Plugin/Action/CreatePdfDerivative.php +++ b/src/Plugin/Action/CreatePdfDerivative.php @@ -18,8 +18,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\File\FileSystemInterface; use Drupal\media\Entity\Media; use Drupal\Core\Session\AccountProxyInterface; - - +use Drupal\media\Entity\MediaType; /** * Provides a Create PDF Derivative action. @@ -116,14 +115,27 @@ final class CreatePdfDerivative extends ConfigurableActionBase implements Contai */ public function execute(ContentEntityInterface $entity = NULL): void { $identifier = $this->configuration['media_use_identifier']; - $service_term = $this->islandoraUtils->getTermForUri('http://pcdm.org/use#ServiceFile'); $pdf_term = $this->islandoraUtils->getTermForUri($identifier); - $media = $this->islandoraUtils->getMediaWithTerm($entity, $service_term); + $terms = [ + $this->islandoraUtils->getTermForUri('http://pcdm.org/use#ServiceFile'), + $this->islandoraUtils->getTermForUri('http://pcdm.org/use#OriginalFile'), + ]; + foreach ($terms as $term) { + $media = $this->islandoraUtils->getMediaWithTerm($entity, $term); + if ($media) { + break; + } + } + if (!$media) { + return; + } + // Original file info. - $field_name = 'field_media_image'; - $source_file = $media->get($field_name)->entity; - $source_uri = $source_file->getFileUri(); // e.g. public://foo/bar.jpg - $source_real = $this->fileSystem->realpath($source_uri); // real path exists + $bundle = $media->bundle(); // string, e.g. 'image' or 'file' + $media_type = MediaType::load($bundle); + $field_name = $media->getSource()->getSourceFieldDefinition($media_type)->getName(); $source_file = $media->get($field_name)->entity; + $source_uri = $source_file->getFileUri(); + $source_real = $this->fileSystem->realpath($source_uri); // Derive PDF names/paths. $pdf_uri = preg_replace('/\.\w+$/', '.pdf', $source_uri); // public://foo/bar.pdf @@ -131,7 +143,7 @@ final class CreatePdfDerivative extends ConfigurableActionBase implements Contai // Run ImageMagick. Prefer absolute path if you know it; leaving as "magick" here. $cmd = sprintf( - 'magick %s %s 2>&1', + 'convert %s %s 2>&1', escapeshellarg($source_real), escapeshellarg($pdf_real), );