Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit b64c13b

Browse files
committed
DW-439: Added drush command
1 parent 4fe6b35 commit b64c13b

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

drush.services.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
os2forms_digital_post.commands:
3+
class: \Drupal\os2forms_digital_post\Commands\CreatePdf
4+
arguments: ['@os2forms_digital_post.template_manager', '@entity_type.manager']
5+
tags:
6+
- { name: drush.command }

src/Commands/CreatePdf.php

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

0 commit comments

Comments
 (0)