Skip to content

Instantly share code, notes, and snippets.

@mohit-rocks
Last active May 16, 2018 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohit-rocks/bafebeeea46b41bdfa136e0ca1e13ace to your computer and use it in GitHub Desktop.
Save mohit-rocks/bafebeeea46b41bdfa136e0ca1e13ace to your computer and use it in GitHub Desktop.
Example hook for how to use Unoconv service. https://drupal.org/project/unoconv_service
<?php
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function unoconv_example_media_presave(EntityInterface $entity) {
$original_file_id = $entity->get('field_document_file')->getValue();
/** @var \Drupal\file\Entity\File $file */
$file = File::load($original_file_id[0]['target_id']);
// Get actual path.
$actual_path = \Drupal::service('file_system')->realpath($file->getFileUri());
// Prepare destination path.
$file_directory_path = \Drupal::service('file_system')->realpath(file_default_scheme() . "://");
$new_filename = pathinfo($file->getFilename(), PATHINFO_FILENAME) . '.pdf';
$destination_path = $file_directory_path . '/preview/' . $new_filename;
$destination_uri = file_default_scheme() . '://preview/' . $new_filename;
$transcode_service = \Drupal::service('unoconv_service.transcode');
$preview_file = $transcode_service->transcode($actual_path, $destination_path, $destination_uri);
$entity->set('field_document_media_preview1', ['target_id' => $preview_file->id()]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment