Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,12 @@ Apply coding standards:
```shell
composer coding-standards-apply
```

## Drush command

A drush command is available for testing purposes. It creates a PDF from a template and a given submission.

Read more:
```shell
drush os2forms_digital_post:create_pdf --help
```
6 changes: 6 additions & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
os2forms_digital_post.commands:
class: \Drupal\os2forms_digital_post\Commands\CreatePdf
arguments: ['@os2forms_digital_post.template_manager', '@entity_type.manager']
tags:
- { name: drush.command }
94 changes: 94 additions & 0 deletions src/Commands/CreatePdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Drupal\os2forms_digital_post\Commands;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drush\Commands\DrushCommands;
use Drupal\os2forms_digital_post\Manager\TemplateManager;

/**
* A drush command file for commands related to os2forms_digital_post.
*
* @package Drupal\event_database_pull\Commands
*/
class CreatePdf extends DrushCommands {

/**
* The os2forms_digital_post template manager.
*
* @var TemplateManager
*/
protected TemplateManager $templateManager;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

public function __construct(TemplateManager $templateManager, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct();
$this->templateManager = $templateManager;
$this->entityTypeManager = $entity_type_manager;
}

/**
* Create PDF in directory relative to Drupal root directory.
*
* @param string $template
* The template name to use.
* @param array $options
* An option that takes multiple values.
*
* @command os2forms_digital_post:create_pdf
* @aliases create-pdf
* @usage os2forms_digital_post:create_pdf default --submission_id=12345
* Create PDF using default template with data from set submission.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function create($template, $options = ['submission_id' => 0, 'file_location' => '', 'file_name' => 'test.pdf']) {
$elements[] = [];
$webformLabel = '';
$webform_submission = $this->entityTypeManager->getStorage('webform_submission')->load($options['submission_id']);
if ($webform_submission) {
$webform = $webform_submission->getWebform();
$webformLabel = $webform->label();
$submissionData = $webform_submission->getData();
foreach ($submissionData as $key => $value) {
$element = $webform->getElement($key, TRUE);
$elements[] = [
'name' => $element['#title'],
'value' => $element['#return_value'] ?? $value,
];
}
}
else {
$this->output()->writeln('Submission id: ' . $options['submission_id'] . ' not found. An empty ' . $template . ' template was created.');
}

$recipient = [
'name' => 'Test Testersen',
'streetName' => 'Testervej',
'streetNumber' => '1',
'floor' => '2',
'side' => 'tv',
'postalCode' => '8000',
'city' => 'Aarhus C.',
];

$context = [
'label' => $webformLabel,
'elements' => $elements,
'recipient' => $recipient,
];

$pathToTemplate = $template;
$pdf = $this->templateManager->renderPdf($pathToTemplate, $context);
$filePath = dirname(DRUPAL_ROOT) . $options['file_location'] . '/' . $options['file_name'];
file_put_contents($filePath, $pdf);
$this->output()->writeln(sprintf('Pdf written to %s', $filePath));
}
}
3 changes: 2 additions & 1 deletion src/Plugin/WebformHandler/DigitalPostWebformHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,9 @@ public function postSave(WebformSubmissionInterface $webform_submission, $update
$element = $this->webform->getElement($key);

$elements[] = [
'label' => $this->webform->label(),
'name' => $element['#title'],
'value' => $value,
'value' => $element['#return_value'] ?? $value,
];
}

Expand Down