From 7fb53321996b7f252b839bb8f633e62d3b6eb11b Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Thu, 19 Feb 2026 10:45:35 +0100 Subject: [PATCH 1/3] Made digital signature text placement on attachment configurable --- .../src/Element/AttachmentElement.php | 5 ++- .../src/Os2formsAttachmentPrintBuilder.php | 44 ++++++++++++++----- .../WebformElement/AttachmentElement.php | 19 ++++++++ 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/modules/os2forms_attachment/src/Element/AttachmentElement.php b/modules/os2forms_attachment/src/Element/AttachmentElement.php index cdf3ae44..bbc33f0b 100644 --- a/modules/os2forms_attachment/src/Element/AttachmentElement.php +++ b/modules/os2forms_attachment/src/Element/AttachmentElement.php @@ -2,6 +2,7 @@ namespace Drupal\os2forms_attachment\Element; +use Drupal\os2forms_attachment\Os2formsAttachmentPrintBuilder; use Drupal\webform\Entity\WebformSubmission; use Drupal\webform\WebformSubmissionInterface; use Drupal\webform_attachment\Element\WebformAttachmentBase; @@ -21,6 +22,7 @@ public function getInfo() { '#view_mode' => 'html', '#export_type' => 'pdf', '#digital_signature' => FALSE, + '#digital_signature_position' => Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT, '#template' => '', ]; } @@ -77,7 +79,8 @@ public static function getFileContent(array $element, WebformSubmissionInterface // Adding digital signature. if (isset($element['#digital_signature']) && $element['#digital_signature']) { - $file_path = $print_builder->savePrintableDigitalSignature([$webform_submission], $print_engine, $scheme, $file_name); + $signaturePosition = $element['#digital_signature_position'] ?? Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT; + $file_path = $print_builder->savePrintableDigitalSignature([$webform_submission], $print_engine, $scheme, $file_name, TRUE, $signaturePosition); } else { $file_path = $print_builder->savePrintable([$webform_submission], $print_engine, $scheme, $file_name); diff --git a/modules/os2forms_attachment/src/Os2formsAttachmentPrintBuilder.php b/modules/os2forms_attachment/src/Os2formsAttachmentPrintBuilder.php index 1f8a9f38..6698b165 100644 --- a/modules/os2forms_attachment/src/Os2formsAttachmentPrintBuilder.php +++ b/modules/os2forms_attachment/src/Os2formsAttachmentPrintBuilder.php @@ -18,6 +18,11 @@ */ class Os2formsAttachmentPrintBuilder extends PrintBuilder { + public const SIGNATURE_POSITION_FOOTER = 'footer'; + public const SIGNATURE_POSITION_HEADER = 'header'; + public const SIGNATURE_POSITION_AFTER_CONTENT = 'after_content'; + public const SIGNATURE_POSITION_BEFORE_CONTENT = 'before_content'; + /** * {@inheritdoc} */ @@ -52,8 +57,8 @@ public function printHtml(EntityInterface $entity, $use_default_css = TRUE, $opt * @return string * FALSE or the URI to the file. E.g. public://my-file.pdf. */ - public function savePrintableDigitalSignature(array $entities, PrintEngineInterface $print_engine, $scheme = 'public', $filename = FALSE, $use_default_css = TRUE) { - $renderer = $this->prepareRenderer($entities, $print_engine, $use_default_css, TRUE); + public function savePrintableDigitalSignature(array $entities, PrintEngineInterface $print_engine, $scheme = 'public', $filename = FALSE, $use_default_css = TRUE, string $signaturePosition = self::SIGNATURE_POSITION_AFTER_CONTENT) { + $renderer = $this->prepareRenderer($entities, $print_engine, $use_default_css, $signaturePosition); // Allow other modules to alter the generated Print object. $this->dispatcher->dispatch(new PreSendPrintEvent($print_engine, $entities), PrintEvents::PRE_SEND); @@ -82,15 +87,16 @@ public function savePrintableDigitalSignature(array $entities, PrintEngineInterf * The print engine. * @param bool $use_default_css * TRUE if we want the default CSS included. - * @param bool $digitalSignature - * If the digital signature message needs to be added. + * @param string $signaturePosition + * The position for the digital signature validation text. Empty string + * means no signature. Use the SIGNATURE_POSITION_* class constants. * * @return \Drupal\entity_print\Renderer\RendererInterface * A print renderer. * * @see PrintBuilder::prepareRenderer */ - protected function prepareRenderer(array $entities, PrintEngineInterface $print_engine, $use_default_css, $digitalSignature = FALSE) { + protected function prepareRenderer(array $entities, PrintEngineInterface $print_engine, $use_default_css, string $signaturePosition = '') { if (empty($entities)) { throw new \InvalidArgumentException('You must pass at least 1 entity'); } @@ -106,12 +112,30 @@ protected function prepareRenderer(array $entities, PrintEngineInterface $print_ '#attached' => [], ]; - // Adding hardcoded negative margin to avoid margins in
- // structure. That margin is automatically added in PDF and PDF only. $generatedHtml = (string) $renderer->generateHtml($entities, $render, $use_default_css, TRUE); - $generatedHtml .= ""; - if ($digitalSignature) { - $generatedHtml .= $this->t('You can validate the signature on this PDF file via validering.nemlog-in.dk.'); + + // Place signature according to the passed parameter. + if ($signaturePosition) { + $signatureHtml = '

' . $this->t('You can validate the signature on this PDF file via validering.nemlog-in.dk.') . '

'; + + switch ($signaturePosition) { + case self::SIGNATURE_POSITION_HEADER: + $generatedHtml = str_replace('', $signatureHtml . '', $generatedHtml); + break; + + case self::SIGNATURE_POSITION_BEFORE_CONTENT: + $generatedHtml = str_replace('
', '
' . $signatureHtml, $generatedHtml); + break; + + case self::SIGNATURE_POSITION_FOOTER: + $generatedHtml = str_replace('', $signatureHtml . '', $generatedHtml); + break; + + case self::SIGNATURE_POSITION_AFTER_CONTENT: + default: + $generatedHtml = str_replace('', $signatureHtml . '', $generatedHtml); + break; + } } $print_engine->addPage($generatedHtml); diff --git a/modules/os2forms_attachment/src/Plugin/WebformElement/AttachmentElement.php b/modules/os2forms_attachment/src/Plugin/WebformElement/AttachmentElement.php index 7ec580bd..3f6a11da 100644 --- a/modules/os2forms_attachment/src/Plugin/WebformElement/AttachmentElement.php +++ b/modules/os2forms_attachment/src/Plugin/WebformElement/AttachmentElement.php @@ -5,6 +5,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\webform\Twig\WebformTwigExtension; use Drupal\webform\Utility\WebformElementHelper; +use Drupal\os2forms_attachment\Os2formsAttachmentPrintBuilder; use Drupal\webform_attachment\Plugin\WebformElement\WebformAttachmentBase; /** @@ -28,6 +29,7 @@ protected function defineDefaultProperties() { 'template' => '', 'export_type' => '', 'digital_signature' => '', + 'digital_signature_position' => 'after_content', 'exclude_empty' => '', 'exclude_empty_checkbox' => '', 'excluded_elements' => '', @@ -93,6 +95,23 @@ public function form(array $form, FormStateInterface $form_state) { '#type' => 'checkbox', '#title' => $this->t('Digital signature'), ]; + $form['attachment']['digital_signature_position'] = [ + '#type' => 'select', + '#title' => $this->t('Digital signature position'), + '#description' => $this->t('Select where the digital signature validation text should be placed in the PDF document.'), + '#options' => [ + Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_FOOTER => $this->t('Footer (repeats on every page)'), + Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_HEADER => $this->t('Header (repeats on every page)'), + Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT => $this->t('After content (end of document)'), + Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_BEFORE_CONTENT => $this->t('Before content (start of document)'), + ], + '#default_value' => Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT, + '#states' => [ + 'visible' => [ + ':input[name="properties[digital_signature]"]' => ['checked' => TRUE], + ], + ], + ]; // Set #access so that help is always visible. WebformElementHelper::setPropertyRecursive($form['attachment']['help'], '#access', TRUE); From 96c578c8c21e92ce5b388c50009a75ae5dd4f83d Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Thu, 19 Feb 2026 10:51:57 +0100 Subject: [PATCH 2/3] Comment and CHANGELOG --- CHANGELOG.md | 3 +++ .../src/Os2formsAttachmentPrintBuilder.php | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6f3e756..14bd0633 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ before starting to add changes. Use example [placed in the end of the page](#exa ## [Unreleased] +- [PR-306](https://github.com/OS2Forms/os2forms/pull/306) + Made digital signature text placement configurable. + ## [5.0.0] 2025-11-18 - [PR-192](https://github.com/OS2Forms/os2forms/pull/192) diff --git a/modules/os2forms_attachment/src/Os2formsAttachmentPrintBuilder.php b/modules/os2forms_attachment/src/Os2formsAttachmentPrintBuilder.php index 6698b165..c72f098c 100644 --- a/modules/os2forms_attachment/src/Os2formsAttachmentPrintBuilder.php +++ b/modules/os2forms_attachment/src/Os2formsAttachmentPrintBuilder.php @@ -49,8 +49,8 @@ public function printHtml(EntityInterface $entity, $use_default_css = TRUE, $opt /** * Modified version of the original savePrintable() function. * - * The only difference is modified call to prepareRenderer with digitalPost - * flag TRUE. + * The only difference is modified call to prepareRenderer with a + * signature position parameter. * * @see PrintBuilder::savePrintable() * From f972417f8d7f9172c1d6779d39e2201d467c94b1 Mon Sep 17 00:00:00 2001 From: jekuaitk Date: Thu, 19 Feb 2026 10:56:17 +0100 Subject: [PATCH 3/3] Use constants --- .../src/Plugin/WebformElement/AttachmentElement.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/os2forms_attachment/src/Plugin/WebformElement/AttachmentElement.php b/modules/os2forms_attachment/src/Plugin/WebformElement/AttachmentElement.php index 3f6a11da..e19c531a 100644 --- a/modules/os2forms_attachment/src/Plugin/WebformElement/AttachmentElement.php +++ b/modules/os2forms_attachment/src/Plugin/WebformElement/AttachmentElement.php @@ -29,7 +29,7 @@ protected function defineDefaultProperties() { 'template' => '', 'export_type' => '', 'digital_signature' => '', - 'digital_signature_position' => 'after_content', + 'digital_signature_position' => Os2formsAttachmentPrintBuilder::SIGNATURE_POSITION_AFTER_CONTENT, 'exclude_empty' => '', 'exclude_empty_checkbox' => '', 'excluded_elements' => '',