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

Commit 41818e3

Browse files
committed
DW-439: Changed stuff after code review
1 parent 94b4ada commit 41818e3

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
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+
```

src/Commands/CreatePdf.php

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
use Drupal\Core\Entity\EntityTypeManagerInterface;
66
use Drush\Commands\DrushCommands;
77
use Drupal\os2forms_digital_post\Manager\TemplateManager;
8-
use Drupal\Core\Config\ConfigFactoryInterface;
98

109
/**
11-
* A drush command file.
10+
* A drush command file for commands related to os2forms_digital_post.
1211
*
1312
* @package Drupal\event_database_pull\Commands
1413
*/
@@ -35,7 +34,7 @@ public function __construct(TemplateManager $templateManager, EntityTypeManagerI
3534
}
3635

3736
/**
38-
* Create PDF named test.pdf in current directory.
37+
* Create PDF in directory relative to Drupal root directory.
3938
*
4039
* @param string $template
4140
* The template name to use.
@@ -50,30 +49,28 @@ public function __construct(TemplateManager $templateManager, EntityTypeManagerI
5049
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
5150
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
5251
*/
53-
public function create($template, $options = ['submission_id'=> 0]) {
52+
public function create($template, $options = ['submission_id' => 0, 'file_location' => '', 'file_name' => 'test.pdf']) {
5453
$elements[] = [];
5554
$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.');
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+
];
7266
}
7367
}
68+
else {
69+
$this->output()->writeln('Submission id: ' . $options['submission_id'] . ' not found. An empty ' . $template . ' template was created.');
70+
}
7471

7572
$recipient = [
76-
'name' => 'Test testersen',
73+
'name' => 'Test Testersen',
7774
'streetName' => 'Testervej',
7875
'streetNumber' => '1',
7976
'floor' => '2',
@@ -90,6 +87,8 @@ public function create($template, $options = ['submission_id'=> 0]) {
9087

9188
$pathToTemplate = $template;
9289
$pdf = $this->templateManager->renderPdf($pathToTemplate, $context);
93-
file_put_contents('test.pdf', $pdf);
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));
9493
}
9594
}

src/Plugin/WebformHandler/DigitalPostWebformHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public function postSave(WebformSubmissionInterface $webform_submission, $update
316316
$elements[] = [
317317
'label' => $this->webform->label(),
318318
'name' => $element['#title'],
319-
'value' => isset($element['#return_value']) ? $element['#return_value'] : $value,
319+
'value' => $element['#return_value'] ?? $value,
320320
];
321321
}
322322

0 commit comments

Comments
 (0)