Skip to content

Commit 5fdc5d4

Browse files
committed
Adjusted when context is set and removed
1 parent eeba74b commit 5fdc5d4

5 files changed

Lines changed: 19 additions & 19 deletions

File tree

modules/os2forms_digital_post/os2forms_digital_post.services.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ services:
2121
- "@Drupal\\os2forms_digital_post\\Helper\\Settings"
2222
- "@plugin.manager.element_info"
2323
- "@webform.token_manager"
24+
- "@Drupal\\os2forms_digital_post\\EventSubscriber\\Os2formsDigitalPostSubscriber"
2425

2526
Drupal\os2forms_digital_post\Helper\ForsendelseHelper:
2627
arguments:
2728
- "@Drupal\\os2forms_digital_post\\Helper\\Settings"
2829
- "@plugin.manager.element_info"
2930
- "@webform.token_manager"
31+
- "@Drupal\\os2forms_digital_post\\EventSubscriber\\Os2formsDigitalPostSubscriber"
3032

3133
Drupal\os2forms_digital_post\Helper\DigitalPostHelper:
3234
arguments:
@@ -52,7 +54,6 @@ services:
5254
- "@logger.channel.os2forms_digital_post"
5355
- "@logger.channel.os2forms_digital_post_submission"
5456
- "@Drupal\\os2forms_digital_post\\Helper\\DigitalPostHelper"
55-
- "@Drupal\\os2forms_digital_post\\EventSubscriber\\Os2formsDigitalPostSubscriber"
5657

5758
Drupal\os2forms_digital_post\Helper\SF1461Helper:
5859

modules/os2forms_digital_post/src/Helper/AbstractMessageHelper.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use DigitalPost\MeMo\Message;
66
use Drupal\Core\Render\ElementInfoManager;
7+
use Drupal\os2forms_digital_post\EventSubscriber\Os2formsDigitalPostSubscriber;
78
use Drupal\os2forms_digital_post\Exception\InvalidAttachmentElementException;
89
use Drupal\os2forms_digital_post\Model\Document;
910
use Drupal\os2forms_digital_post\Plugin\WebformHandler\WebformHandlerSF1601;
11+
use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult;
12+
use Drupal\os2web_datalookup\LookupResult\CprLookupResult;
1013
use Drupal\webform\WebformSubmissionInterface;
1114
use Drupal\webform\WebformTokenManagerInterface;
1215
use Drupal\webform_attachment\Element\WebformAttachmentBase;
@@ -28,6 +31,7 @@ public function __construct(
2831
readonly protected ElementInfoManager $elementInfoManager,
2932
#[Autowire(service: 'webform.token_manager')]
3033
readonly protected WebformTokenManagerInterface $webformTokenManager,
34+
readonly protected Os2formsDigitalPostSubscriber $digitalPostSubscriber,
3135
) {
3236
}
3337

@@ -38,7 +42,7 @@ public function __construct(
3842
*
3943
* @phpstan-param array<string, mixed> $handlerSettings
4044
*/
41-
protected function getMainDocument(WebformSubmissionInterface $submission, array $handlerSettings): Document {
45+
protected function getMainDocument(WebformSubmissionInterface $submission, array $handlerSettings, CprLookupResult|CompanyLookupResult $recipientData): Document {
4246
// Lifted from Drupal\webform_attachment\Controller\WebformAttachmentController::download.
4347
$element = $handlerSettings[WebformHandlerSF1601::MEMO_MESSAGE][WebformHandlerSF1601::ATTACHMENT_ELEMENT];
4448
$element = $submission->getWebform()->getElement($element) ?: [];
@@ -51,7 +55,15 @@ protected function getMainDocument(WebformSubmissionInterface $submission, array
5155

5256
$fileName = $instance::getFileName($element, $submission);
5357
$mimeType = $instance::getFileMimeType($element, $submission);
58+
59+
// The way to alter html generated from entities is through the
60+
// PrintEvents::POST_RENDER event. See:
61+
// @Drupal\entity_print\Renderer::generateHtml,
62+
// To indicate digital post context and get the necessary information,
63+
// we add a flag to the session.
64+
$this->digitalPostSubscriber->setDigitalPostContext($submission, $recipientData);
5465
$content = $instance::getFileContent($element, $submission);
66+
$this->digitalPostSubscriber->deleteDigitalPostContext($submission);
5567

5668
return new Document(
5769
$content,

modules/os2forms_digital_post/src/Helper/ForsendelseHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function buildForsendelse(CprLookupResult|CompanyLookupResult $recipientD
6161
*/
6262
public function buildSubmissionForsendelse(WebformSubmissionInterface $submission, array $options, array $handlerSettings, CprLookupResult|CompanyLookupResult $recipientData): ForsendelseI {
6363
$label = $this->replaceTokens($options[WebformHandlerSF1601::MESSAGE_HEADER_LABEL], $submission);
64-
$document = $this->getMainDocument($submission, $handlerSettings);
64+
$document = $this->getMainDocument($submission, $handlerSettings, $recipientData);
6565

6666
return $this->buildForsendelse($recipientData, $label, $document);
6767
}

modules/os2forms_digital_post/src/Helper/MeMoHelper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ public function buildMessage(CprLookupResult|CompanyLookupResult $recipientData,
9797
* @phpstan-param array<string, mixed> $options
9898
* @phpstan-param array<string, mixed> $handlerSettings
9999
*/
100-
public function buildWebformSubmissionMessage(WebformSubmissionInterface $submission, array $options, array $handlerSettings, CprLookupResult|CompanyLookupResult|null $recipientData = NULL): Message {
100+
public function buildWebformSubmissionMessage(WebformSubmissionInterface $submission, array $options, array $handlerSettings, CprLookupResult|CompanyLookupResult $recipientData): Message {
101101
$senderLabel = $this->replaceTokens($options[WebformHandlerSF1601::SENDER_LABEL], $submission);
102102
$messageLabel = $this->replaceTokens($options[WebformHandlerSF1601::MESSAGE_HEADER_LABEL], $submission);
103-
$document = $this->getMainDocument($submission, $handlerSettings);
103+
$document = $this->getMainDocument($submission, $handlerSettings, $recipientData);
104+
104105
$actions = [];
105106
if (isset($handlerSettings[WebformHandlerSF1601::MEMO_ACTIONS]['actions'])) {
106107
foreach ($handlerSettings[WebformHandlerSF1601::MEMO_ACTIONS]['actions'] as $spec) {

modules/os2forms_digital_post/src/Helper/WebformHelperSF1601.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
namespace Drupal\os2forms_digital_post\Helper;
44

5-
use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult;
6-
use Drupal\os2web_datalookup\LookupResult\CprLookupResult;
75
use Drupal\Core\Entity\EntityStorageInterface;
86
use Drupal\Core\Entity\EntityTypeManagerInterface;
97
use Drupal\Core\Logger\LoggerChannelInterface;
108
use Drupal\advancedqueue\Entity\QueueInterface;
119
use Drupal\advancedqueue\Job;
1210
use Drupal\advancedqueue\JobResult;
13-
use Drupal\os2forms_digital_post\EventSubscriber\Os2formsDigitalPostSubscriber;
1411
use Drupal\os2forms_digital_post\Exception\InvalidRecipientIdentifierElementException;
1512
use Drupal\os2forms_digital_post\Exception\RuntimeException;
1613
use Drupal\os2forms_digital_post\Exception\SubmissionNotFoundException;
@@ -65,7 +62,6 @@ public function __construct(
6562
#[Autowire(service: 'logger.channel.os2forms_digital_post_submission')]
6663
private readonly LoggerChannelInterface $submissionLogger,
6764
private readonly DigitalPostHelper $digitalPostHelper,
68-
private readonly Os2formsDigitalPostSubscriber $digitalPostSubscriber,
6965
) {
7066
$this->webformSubmissionStorage = $entityTypeManager->getStorage('webform_submission');
7167
$this->queueStorage = $entityTypeManager->getStorage('advancedqueue_queue');
@@ -156,8 +152,6 @@ public function sendDigitalPost(WebformSubmissionInterface $submission, array $h
156152
$recipientIdentifierType = 'CPR';
157153
}
158154

159-
160-
161155
$senderSettings = $this->settings->getSender();
162156
$messageOptions = [
163157
self::RECIPIENT_IDENTIFIER_TYPE => $recipientIdentifierType,
@@ -170,11 +164,7 @@ public function sendDigitalPost(WebformSubmissionInterface $submission, array $h
170164
WebformHandlerSF1601::MESSAGE_HEADER_LABEL => $handlerMessageSettings[WebformHandlerSF1601::MESSAGE_HEADER_LABEL],
171165
];
172166

173-
// Set flag indicating digital post context.
174-
$this->digitalPostSubscriber->setDigitalPostContext($submission, $lookupResult);
175167
$message = $this->meMoHelper->buildWebformSubmissionMessage($submission, $messageOptions, $handlerSettings, $lookupResult);
176-
// Remove flag.
177-
$this->digitalPostSubscriber->deleteDigitalPostContext($submission);
178168

179169
$forsendelseOptions = [
180170
self::RECIPIENT_IDENTIFIER_TYPE => $recipientIdentifierType,
@@ -188,11 +178,7 @@ public function sendDigitalPost(WebformSubmissionInterface $submission, array $h
188178
WebformHandlerSF1601::MESSAGE_HEADER_LABEL => $handlerMessageSettings[WebformHandlerSF1601::MESSAGE_HEADER_LABEL],
189179
];
190180

191-
// Set flag indicating digital post context.
192-
$this->digitalPostSubscriber->setDigitalPostContext($submission, $lookupResult);
193181
$forsendelse = $this->forsendelseHelper->buildSubmissionForsendelse($submission, $forsendelseOptions, $handlerSettings, $lookupResult);
194-
// Remove flag.
195-
$this->digitalPostSubscriber->deleteDigitalPostContext($submission);
196182

197183
$type = $handlerMessageSettings[WebformHandlerSF1601::TYPE] ?? SF1601::TYPE_DIGITAL_POST;
198184

0 commit comments

Comments
 (0)