Browse Source

added fallback

main
astanley 2 weeks ago
parent
commit
35a62a4c59
  1. 30
      src/Plugin/Action/CreatePdfDerivative.php

30
src/Plugin/Action/CreatePdfDerivative.php

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

Loading…
Cancel
Save