|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\os2forms_digital_post\Commands; |
| 4 | + |
| 5 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 6 | +use Drush\Commands\DrushCommands; |
| 7 | +use Drupal\os2forms_digital_post\Manager\TemplateManager; |
| 8 | + |
| 9 | +/** |
| 10 | + * A drush command file for commands related to os2forms_digital_post. |
| 11 | + * |
| 12 | + * @package Drupal\event_database_pull\Commands |
| 13 | + */ |
| 14 | +class CreatePdf extends DrushCommands { |
| 15 | + |
| 16 | + /** |
| 17 | + * The os2forms_digital_post template manager. |
| 18 | + * |
| 19 | + * @var TemplateManager |
| 20 | + */ |
| 21 | + protected TemplateManager $templateManager; |
| 22 | + |
| 23 | + /** |
| 24 | + * The entity type manager. |
| 25 | + * |
| 26 | + * @var \Drupal\Core\Entity\EntityTypeManagerInterface |
| 27 | + */ |
| 28 | + protected $entityTypeManager; |
| 29 | + |
| 30 | + public function __construct(TemplateManager $templateManager, EntityTypeManagerInterface $entity_type_manager) { |
| 31 | + parent::__construct(); |
| 32 | + $this->templateManager = $templateManager; |
| 33 | + $this->entityTypeManager = $entity_type_manager; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Create PDF in directory relative to Drupal root directory. |
| 38 | + * |
| 39 | + * @param string $template |
| 40 | + * The template name to use. |
| 41 | + * @param array $options |
| 42 | + * An option that takes multiple values. |
| 43 | + * |
| 44 | + * @command os2forms_digital_post:create_pdf |
| 45 | + * @aliases create-pdf |
| 46 | + * @usage os2forms_digital_post:create_pdf default --submission_id=12345 |
| 47 | + * Create PDF using default template with data from set submission. |
| 48 | + * |
| 49 | + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException |
| 50 | + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException |
| 51 | + */ |
| 52 | + public function create($template, $options = ['submission_id' => 0, 'file_location' => '', 'file_name' => 'test.pdf']) { |
| 53 | + $elements[] = []; |
| 54 | + $webformLabel = ''; |
| 55 | + $webform_submission = $this->entityTypeManager->getStorage('webform_submission')->load($options['submission_id']); |
| 56 | + if ($webform_submission) { |
| 57 | + $webform = $webform_submission->getWebform(); |
| 58 | + $webformLabel = $webform->label(); |
| 59 | + $submissionData = $webform_submission->getData(); |
| 60 | + foreach ($submissionData as $key => $value) { |
| 61 | + $element = $webform->getElement($key, TRUE); |
| 62 | + $elements[] = [ |
| 63 | + 'name' => $element['#title'], |
| 64 | + 'value' => $element['#return_value'] ?? $value, |
| 65 | + ]; |
| 66 | + } |
| 67 | + } |
| 68 | + else { |
| 69 | + $this->output()->writeln('Submission id: ' . $options['submission_id'] . ' not found. An empty ' . $template . ' template was created.'); |
| 70 | + } |
| 71 | + |
| 72 | + $recipient = [ |
| 73 | + 'name' => 'Test Testersen', |
| 74 | + 'streetName' => 'Testervej', |
| 75 | + 'streetNumber' => '1', |
| 76 | + 'floor' => '2', |
| 77 | + 'side' => 'tv', |
| 78 | + 'postalCode' => '8000', |
| 79 | + 'city' => 'Aarhus C.', |
| 80 | + ]; |
| 81 | + |
| 82 | + $context = [ |
| 83 | + 'label' => $webformLabel, |
| 84 | + 'elements' => $elements, |
| 85 | + 'recipient' => $recipient, |
| 86 | + ]; |
| 87 | + |
| 88 | + $pathToTemplate = $template; |
| 89 | + $pdf = $this->templateManager->renderPdf($pathToTemplate, $context); |
| 90 | + $filePath = dirname(DRUPAL_ROOT) . $options['file_location'] . '/' . $options['file_name']; |
| 91 | + file_put_contents($filePath, $pdf); |
| 92 | + $this->output()->writeln(sprintf('Pdf written to %s', $filePath)); |
| 93 | + } |
| 94 | +} |
0 commit comments