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

Commit 5d7ffd4

Browse files
authored
Merge pull request #13 from itk-dev/feature/drush-command
Feature/drush command
2 parents 4fe6b35 + 41818e3 commit 5d7ffd4

File tree

4 files changed

+111
-1
lines changed

4 files changed

+111
-1
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,12 @@ Apply coding standards:
9090
```shell
9191
composer coding-standards-apply
9292
```
93+
94+
## Drush command
95+
96+
A drush command is available for testing purposes. It creates a PDF from a template and a given submission.
97+
98+
Read more:
99+
```shell
100+
drush os2forms_digital_post:create_pdf --help
101+
```

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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

src/Plugin/WebformHandler/DigitalPostWebformHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,9 @@ public function postSave(WebformSubmissionInterface $webform_submission, $update
314314
$element = $this->webform->getElement($key);
315315

316316
$elements[] = [
317+
'label' => $this->webform->label(),
317318
'name' => $element['#title'],
318-
'value' => $value,
319+
'value' => $element['#return_value'] ?? $value,
319320
];
320321
}
321322

0 commit comments

Comments
 (0)