Skip to content

Commit 10403c2

Browse files
committed
PDF/Digital post hack
1 parent 3c2c793 commit 10403c2

File tree

3 files changed

+131
-15
lines changed

3 files changed

+131
-15
lines changed

web/modules/custom/os2forms_maestro_webform/os2forms_maestro_webform.info.yml

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ package: OS2Forms
55
type: module
66

77
configure: os2forms_maestro_webform.settings
8+
9+
dependencies:
10+
- os2forms_attachment:os2forms_attachment

web/modules/custom/os2forms_maestro_webform/os2forms_maestro_webform.module

+17-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use Drupal\os2forms_maestro_webform\MaestroHelper;
1313
* Implements hook_maestro_zero_user_notification().
1414
*/
1515
function os2forms_maestro_webform_maestro_zero_user_notification($templateMachineName, $taskMachineName, $queueID, $notificationType) {
16-
Drupal::service(MaestroHelper::class)->maestroZeroUserNotification($templateMachineName, $taskMachineName, $queueID, $notificationType);
16+
_os2forms_maestro_webform_helper()->maestroZeroUserNotification($templateMachineName, $taskMachineName, $queueID, $notificationType);
1717
}
1818

1919
/**
@@ -107,12 +107,26 @@ function os2forms_maestro_webform_form_alter(&$form, FormStateInterface $form_st
107107
* sessions, login tokens, email addresses etc.
108108
*/
109109
function os2forms_maestro_webform_maestro_can_user_execute_task_alter(&$returnValue, $queueID, $userID) {
110-
Drupal::service(MaestroHelper::class)->maestroCanUserExecuteTaskAlter($returnValue, $queueID, $userID);
110+
_os2forms_maestro_webform_helper()->maestroCanUserExecuteTaskAlter($returnValue, $queueID, $userID);
111111
}
112112

113113
/**
114114
* Implements hook_mail().
115115
*/
116116
function os2forms_maestro_webform_mail($key, &$message, $params) {
117-
Drupal::service(MaestroHelper::class)->mail($key, $message, $params);
117+
_os2forms_maestro_webform_helper()->mail($key, $message, $params);
118+
}
119+
120+
/**
121+
* Implements hook_preprocess_entity_print().
122+
*/
123+
function os2forms_maestro_webform_preprocess_entity_print(array &$variables) {
124+
_os2forms_maestro_webform_helper()->preprocessEntityPrint($variables);
125+
}
126+
127+
/**
128+
* Get MaestroHelper.
129+
*/
130+
function _os2forms_maestro_webform_helper(): MaestroHelper {
131+
return Drupal::service(MaestroHelper::class);
118132
}

web/modules/custom/os2forms_maestro_webform/src/MaestroHelper.php

+111-12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Drupal\Core\Entity\EntityTypeManagerInterface;
99
use Drupal\Core\Mail\MailManagerInterface;
1010
use Drupal\maestro\Engine\MaestroEngine;
11+
use Drupal\os2forms_attachment\Element\AttachmentElement;
1112
use Drupal\os2forms_maestro_webform\Form\SettingsForm;
1213
use Drupal\os2forms_maestro_webform\Plugin\WebformHandler\NotificationHandler;
1314
use Drupal\webform\WebformSubmissionInterface;
@@ -18,6 +19,8 @@
1819
* Maestro helper.
1920
*/
2021
class MaestroHelper {
22+
private const OS2FORMS_MAESTRO_WEBFORM_IS_NOTIFICATION = 'os2forms_maestro_webform_is_notification';
23+
private const OS2FORMS_MAESTRO_WEBFORM_NOTIFICATION_CONTENT = 'os2forms_maestro_webform_notification_content';
2124
/**
2225
* The config.
2326
*
@@ -83,7 +86,7 @@ public function maestroCanUserExecuteTaskAlter(bool &$returnValue, int $queueID,
8386
$assignments = explode(',', $templateTask['assigned']);
8487

8588
$knownAnonymousAssignments = array_map(
86-
static fn (string $role) => 'role:fixed:' . $role,
89+
static fn(string $role) => 'role:fixed:' . $role,
8790
array_filter($this->config->get('known_anonymous_roles') ?: [])
8891
);
8992

@@ -111,7 +114,11 @@ public function maestroCanUserExecuteTaskAlter(bool &$returnValue, int $queueID,
111114
/**
112115
* Handle submission notification.
113116
*/
114-
private function handleSubmissionNotification(WebformSubmissionInterface $submission, array $templateTask, int $queueID): void {
117+
private function handleSubmissionNotification(
118+
WebformSubmissionInterface $submission,
119+
array $templateTask,
120+
int $queueID
121+
): void {
115122
$data = $submission->getData();
116123
$webform = $submission->getWebform();
117124
$handlers = $webform->getHandlers('os2forms_maestro_webform_notification');
@@ -151,10 +158,10 @@ private function handleSubmissionNotification(WebformSubmissionInterface $submis
151158
}
152159

153160
if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
154-
$this->sendNotificationEmail($recipient, $subject, $content);
161+
$this->sendNotificationEmail($recipient, $subject, $content, $submission);
155162
}
156163
else {
157-
$this->sendNotificationDigitalPost($recipient, $subject, $content);
164+
$this->sendNotificationDigitalPost($recipient, $subject, $content, $submission);
158165
}
159166
}
160167
}
@@ -163,7 +170,12 @@ private function handleSubmissionNotification(WebformSubmissionInterface $submis
163170
/**
164171
* Send notification email.
165172
*/
166-
private function sendNotificationEmail(string $recipient, string $subject, string $content): void {
173+
private function sendNotificationEmail(
174+
string $recipient,
175+
string $subject,
176+
string $content,
177+
WebformSubmissionInterface $submission
178+
): void {
167179
$result = $this->mailManager->mail(
168180
'os2forms_maestro_webform',
169181
'notification',
@@ -174,25 +186,112 @@ private function sendNotificationEmail(string $recipient, string $subject, strin
174186
'body' => $content,
175187
]
176188
);
189+
190+
if (!$result['result']) {
191+
// @todo Log this error.
192+
}
177193
}
178194

179195
/**
180196
* Send notification digital post.
181197
*/
182-
private function sendNotificationDigitalPost(string $recipient, string $subject, string $content): void {
183-
$this->sendNotificationEmail(
184-
$recipient . '@digital-post.example.com',
185-
'(this should have been a digital post)' . $subject,
186-
$content);
198+
private function sendNotificationDigitalPost(
199+
string $recipient,
200+
string $subject,
201+
string $content,
202+
WebformSubmissionInterface $submission
203+
): void {
204+
$element = [
205+
// Cf. AttachmentElement::getFileContent().
206+
'#view_mode' => 'html',
207+
'#export_type' => 'pdf',
208+
];
209+
210+
$submission->setData($submission->getData() + [
211+
self::OS2FORMS_MAESTRO_WEBFORM_IS_NOTIFICATION => TRUE,
212+
self::OS2FORMS_MAESTRO_WEBFORM_NOTIFICATION_CONTENT => $content,
213+
]);
214+
215+
$content = AttachmentElement::getFileContent($element, $submission);
216+
217+
// @todo Send real digital post
218+
$recipient .= '@digital-post.example.com';
219+
$subject .= ' (digital post)';
220+
221+
$result = $this->mailManager->mail(
222+
'os2forms_maestro_webform',
223+
'notification',
224+
$recipient,
225+
'',
226+
[
227+
'subject' => $subject,
228+
'body' => $content,
229+
'attachments' => [
230+
[
231+
'filecontent' => $content,
232+
'filename' => 'stuff.pdf',
233+
'filemime' => 'application/pdf',
234+
],
235+
],
236+
]
237+
);
238+
239+
if (!$result['result']) {
240+
// @todo Log this error.
241+
}
187242
}
188243

189-
public function mail(string $key, array &$message, array $params)
190-
{
244+
/**
245+
* Implements hook_mail().
246+
*/
247+
public function mail(string $key, array &$message, array $params) {
191248
switch ($key) {
192249
case 'notification':
193250
$message['subject'] = $params['subject'];
194251
$message['body'][] = Html::escape($params['body']);
252+
if (isset($params['attachments'])) {
253+
foreach ($params['attachments'] as $attachment) {
254+
$message['params']['attachments'][] = $attachment;
255+
}
256+
}
195257
break;
196258
}
197259
}
260+
261+
/**
262+
* Implements hook_preprocess_entity_print().
263+
*/
264+
public function preprocessEntityPrint(array &$variables) {
265+
$submission = $this->getWebformSubmission($variables);
266+
if (NULL === $submission) {
267+
return;
268+
}
269+
$data = $submission->getData();
270+
if (TRUE !== ($data[self::OS2FORMS_MAESTRO_WEBFORM_IS_NOTIFICATION] ?? FALSE)) {
271+
return;
272+
}
273+
274+
$variables['content'] = [
275+
'#markup' => $data[self::OS2FORMS_MAESTRO_WEBFORM_NOTIFICATION_CONTENT] ?? '',
276+
];
277+
}
278+
279+
/**
280+
* Dig for webform submission in variables.
281+
*/
282+
private function getWebformSubmission(array $variables): ?WebformSubmissionInterface {
283+
$iterator = new \RecursiveArrayIterator($variables);
284+
$recursive = new \RecursiveIteratorIterator(
285+
$iterator,
286+
\RecursiveIteratorIterator::SELF_FIRST
287+
);
288+
foreach ($recursive as $key => $value) {
289+
if ('#webform_submission' === $key && $value instanceof WebformSubmissionInterface) {
290+
return $value;
291+
}
292+
}
293+
294+
return NULL;
295+
}
296+
198297
}

0 commit comments

Comments
 (0)