Skip to content

Commit b2031bc

Browse files
authored
Merge pull request #306 from itk-dev/feature/digital-signature-placement
Made digital signature text placement on attachment configurable
2 parents b79b2b1 + 4e39f4b commit b2031bc

4 files changed

Lines changed: 61 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ before starting to add changes. Use example [placed in the end of the page](#exa
1111

1212
## [Unreleased]
1313

14+
- [PR-306](https://github.com/OS2Forms/os2forms/pull/306)
15+
Made digital signature text placement configurable.
1416
- [#251](https://github.com/OS2Forms/os2forms/issues/251)
1517
Webform encrypt uninstall problem fix
1618
- git actions check

modules/os2forms_attachment/src/Element/AttachmentElement.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\os2forms_attachment\Element;
44

5+
use Drupal\os2forms_attachment\Os2formsAttachmentPrintBuilder;
56
use Drupal\webform\Entity\WebformSubmission;
67
use Drupal\webform\WebformSubmissionInterface;
78
use Drupal\webform_attachment\Element\WebformAttachmentBase;
@@ -21,6 +22,7 @@ public function getInfo() {
2122
'#view_mode' => 'html',
2223
'#export_type' => 'pdf',
2324
'#digital_signature' => FALSE,
25+
'#digital_signature_position' => Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT,
2426
'#template' => '',
2527
];
2628
}
@@ -77,7 +79,8 @@ public static function getFileContent(array $element, WebformSubmissionInterface
7779

7880
// Adding digital signature.
7981
if (isset($element['#digital_signature']) && $element['#digital_signature']) {
80-
$file_path = $print_builder->savePrintableDigitalSignature([$webform_submission], $print_engine, $scheme, $file_name);
82+
$signaturePosition = $element['#digital_signature_position'] ?? Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT;
83+
$file_path = $print_builder->savePrintableDigitalSignature([$webform_submission], $print_engine, $scheme, $file_name, TRUE, $signaturePosition);
8184
}
8285
else {
8386
$file_path = $print_builder->savePrintable([$webform_submission], $print_engine, $scheme, $file_name);

modules/os2forms_attachment/src/Os2formsAttachmentPrintBuilder.php

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
*/
1919
class Os2formsAttachmentPrintBuilder extends PrintBuilder {
2020

21+
public const SIGNATURE_POSITION_FOOTER = 'footer';
22+
public const SIGNATURE_POSITION_HEADER = 'header';
23+
public const SIGNATURE_POSITION_AFTER_CONTENT = 'after_content';
24+
public const SIGNATURE_POSITION_BEFORE_CONTENT = 'before_content';
25+
2126
/**
2227
* {@inheritdoc}
2328
*/
@@ -44,16 +49,16 @@ public function printHtml(EntityInterface $entity, $use_default_css = TRUE, $opt
4449
/**
4550
* Modified version of the original savePrintable() function.
4651
*
47-
* The only difference is modified call to prepareRenderer with digitalPost
48-
* flag TRUE.
52+
* The only difference is modified call to prepareRenderer with a
53+
* signature position parameter.
4954
*
5055
* @see PrintBuilder::savePrintable()
5156
*
5257
* @return string
5358
* FALSE or the URI to the file. E.g. public://my-file.pdf.
5459
*/
55-
public function savePrintableDigitalSignature(array $entities, PrintEngineInterface $print_engine, $scheme = 'public', $filename = FALSE, $use_default_css = TRUE) {
56-
$renderer = $this->prepareRenderer($entities, $print_engine, $use_default_css, TRUE);
60+
public function savePrintableDigitalSignature(array $entities, PrintEngineInterface $print_engine, $scheme = 'public', $filename = FALSE, $use_default_css = TRUE, string $signaturePosition = self::SIGNATURE_POSITION_AFTER_CONTENT) {
61+
$renderer = $this->prepareRenderer($entities, $print_engine, $use_default_css, $signaturePosition);
5762

5863
// Allow other modules to alter the generated Print object.
5964
$this->dispatcher->dispatch(new PreSendPrintEvent($print_engine, $entities), PrintEvents::PRE_SEND);
@@ -82,15 +87,16 @@ public function savePrintableDigitalSignature(array $entities, PrintEngineInterf
8287
* The print engine.
8388
* @param bool $use_default_css
8489
* TRUE if we want the default CSS included.
85-
* @param bool $digitalSignature
86-
* If the digital signature message needs to be added.
90+
* @param string $signaturePosition
91+
* The position for the digital signature validation text. Empty string
92+
* means no signature. Use the SIGNATURE_POSITION_* class constants.
8793
*
8894
* @return \Drupal\entity_print\Renderer\RendererInterface
8995
* A print renderer.
9096
*
9197
* @see PrintBuilder::prepareRenderer
9298
*/
93-
protected function prepareRenderer(array $entities, PrintEngineInterface $print_engine, $use_default_css, $digitalSignature = FALSE) {
99+
protected function prepareRenderer(array $entities, PrintEngineInterface $print_engine, $use_default_css, string $signaturePosition = '') {
94100
if (empty($entities)) {
95101
throw new \InvalidArgumentException('You must pass at least 1 entity');
96102
}
@@ -106,12 +112,30 @@ protected function prepareRenderer(array $entities, PrintEngineInterface $print_
106112
'#attached' => [],
107113
];
108114

109-
// Adding hardcoded negative margin to avoid margins in <fieldset> <legend>
110-
// structure. That margin is automatically added in PDF and PDF only.
111115
$generatedHtml = (string) $renderer->generateHtml($entities, $render, $use_default_css, TRUE);
112-
$generatedHtml .= "<style>fieldset legend {margin-left: -12px;}</style>";
113-
if ($digitalSignature) {
114-
$generatedHtml .= $this->t('You can validate the signature on this PDF file via validering.nemlog-in.dk.');
116+
117+
// Place signature according to the passed parameter.
118+
if ($signaturePosition) {
119+
$signatureHtml = '<div class="validate-signature-element"><p>' . $this->t('You can validate the signature on this PDF file via validering.nemlog-in.dk.') . '</p></div>';
120+
121+
switch ($signaturePosition) {
122+
case self::SIGNATURE_POSITION_HEADER:
123+
$generatedHtml = str_replace('</header>', $signatureHtml . '</header>', $generatedHtml);
124+
break;
125+
126+
case self::SIGNATURE_POSITION_BEFORE_CONTENT:
127+
$generatedHtml = str_replace('<div class="page">', '<div class="page">' . $signatureHtml, $generatedHtml);
128+
break;
129+
130+
case self::SIGNATURE_POSITION_FOOTER:
131+
$generatedHtml = str_replace('</footer>', $signatureHtml . '</footer>', $generatedHtml);
132+
break;
133+
134+
case self::SIGNATURE_POSITION_AFTER_CONTENT:
135+
default:
136+
$generatedHtml = str_replace('</body>', $signatureHtml . '</body>', $generatedHtml);
137+
break;
138+
}
115139
}
116140

117141
$print_engine->addPage($generatedHtml);

modules/os2forms_attachment/src/Plugin/WebformElement/AttachmentElement.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Drupal\Core\Form\FormStateInterface;
66
use Drupal\webform\Twig\WebformTwigExtension;
77
use Drupal\webform\Utility\WebformElementHelper;
8+
use Drupal\os2forms_attachment\Os2formsAttachmentPrintBuilder;
89
use Drupal\webform_attachment\Plugin\WebformElement\WebformAttachmentBase;
910

1011
/**
@@ -28,6 +29,7 @@ protected function defineDefaultProperties() {
2829
'template' => '',
2930
'export_type' => '',
3031
'digital_signature' => '',
32+
'digital_signature_position' => Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT,
3133
'exclude_empty' => '',
3234
'exclude_empty_checkbox' => '',
3335
'excluded_elements' => '',
@@ -93,6 +95,23 @@ public function form(array $form, FormStateInterface $form_state) {
9395
'#type' => 'checkbox',
9496
'#title' => $this->t('Digital signature'),
9597
];
98+
$form['attachment']['digital_signature_position'] = [
99+
'#type' => 'select',
100+
'#title' => $this->t('Digital signature position'),
101+
'#description' => $this->t('Select where the digital signature validation text should be placed in the PDF document.'),
102+
'#options' => [
103+
Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_FOOTER => $this->t('Footer (repeats on every page)'),
104+
Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_HEADER => $this->t('Header (repeats on every page)'),
105+
Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT => $this->t('After content (end of document)'),
106+
Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_BEFORE_CONTENT => $this->t('Before content (start of document)'),
107+
],
108+
'#default_value' => Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT,
109+
'#states' => [
110+
'visible' => [
111+
':input[name="properties[digital_signature]"]' => ['checked' => TRUE],
112+
],
113+
],
114+
];
96115

97116
// Set #access so that help is always visible.
98117
WebformElementHelper::setPropertyRecursive($form['attachment']['help'], '#access', TRUE);

0 commit comments

Comments
 (0)