Last active
May 16, 2018 14:30
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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