Skip to content

Commit 64fc6b2

Browse files
committed
Handled custom templates
1 parent 3bc6628 commit 64fc6b2

File tree

6 files changed

+154
-86
lines changed

6 files changed

+154
-86
lines changed

web/sites/default/modules/os2forms_forloeb/os2forms_forloeb.services.yml

+1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ services:
1818
- '@logger.channel.os2forms_forloeb'
1919
- '@logger.channel.os2forms_forloeb_submission'
2020
- '@module_handler'
21+
- '@plugin.manager.entity_print.print_engine'
2122
- '@Drupal\os2forms_digital_post\Helper\DigitalPostHelper'

web/sites/default/modules/os2forms_forloeb/src/Controller/MaestroNotificationController.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public function preview(Request $request, WebformInterface $webform, string $han
8181
$submission = $this->webformSubmissionStorage->load($currentSubmission);
8282
$templateTask = [];
8383
$maestroQueueID = 0;
84-
[,,
85-
$recipient,
86-
$subject,
84+
[
85+
'recipient' => $recipient,
86+
'subject' => $subject,
8787
] = $this->maestroHelper->renderNotification($submission, $handler->getHandlerId(), $notification_type, $templateTask, $maestroQueueID, $content_type);
8888

8989
return [
@@ -107,7 +107,10 @@ public function preview(Request $request, WebformInterface $webform, string $han
107107
public function previewRender(Request $request, WebformInterface $webform, string $handler, string $notification_type, string $content_type, WebformSubmissionInterface $submission) {
108108
$templateTask = [];
109109
$maestroQueueID = 0;
110-
[$content, $contentType] = $this->maestroHelper->renderNotification($submission, $handler, $notification_type, $templateTask, $maestroQueueID, $content_type);
110+
[
111+
'content' => $content,
112+
'contentType' => $contentType,
113+
] = $this->maestroHelper->renderNotification($submission, $handler, $notification_type, $templateTask, $maestroQueueID, $content_type);
111114

112115
$response = new Response($content);
113116
if ('pdf' === $contentType) {

web/sites/default/modules/os2forms_forloeb/src/Form/SettingsForm.php

+11-4
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,25 @@ public function buildForm(array $form, FormStateInterface $form_state) {
110110
$defaultTemplate = file_exists($templatePath) ? file_get_contents($templatePath) : NULL;
111111
$form['templates']['notification_email'] = [
112112
'#type' => 'textarea',
113-
'#rows' => 10,
113+
'#rows' => 20,
114114
'#required' => TRUE,
115115
'#title' => $this->t('Email template'),
116116
'#default_value' => $config->get('templates')['notification_email'] ?? $defaultTemplate,
117-
'#description' => $this->t('HTML template for email notifications. See @template_path'),
117+
'#description' => $this->t('HTML template for email notifications. If the template is a path, e.g. <code>@templatePath</code>, the template will be loaded from this path.', [
118+
'@templatePath' => $templatePath,
119+
]),
118120
];
119121

122+
$templatePath = $this->moduleHandler->getPath('os2forms_forloeb') . '/templates/os2forms-forloeb-notification-message-pdf-html.html.twig';
120123
$form['templates']['notification_pdf'] = [
121124
'#type' => 'textarea',
125+
'#rows' => 20,
122126
'#required' => TRUE,
123127
'#title' => $this->t('PDF template'),
124-
'#default_value' => $config->get('templates')['notification_pdf'] ?? '',
125-
'#description' => $this->t('HTML template for PDF notifications (digital post)'),
128+
'#default_value' => $config->get('templates')['notification_pdf'] ?? $defaultTemplate,
129+
'#description' => $this->t('HTML template for PDF notifications (digital post). If the template is a path, e.g. <code>@templatePath</code>, the template will be loaded from this path.', [
130+
'@templatePath' => $templatePath,
131+
]),
126132
];
127133

128134
return parent::buildForm($form, $form_state);
@@ -135,6 +141,7 @@ public function submitForm(array &$form, FormStateInterface $formState) {
135141
$this->config(static::SETTINGS)
136142
->set('known_anonymous_roles', $formState->getValue('known_anonymous_roles'))
137143
->set('processing', $formState->getValue('processing'))
144+
->set('templates', $formState->getValue('templates'))
138145
->save();
139146

140147
parent::submitForm($form, $formState);

web/sites/default/modules/os2forms_forloeb/src/MaestroHelper.php

+92-66
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use DigitalPost\MeMo\Action;
66
use DigitalPost\MeMo\EntryPoint;
7-
use Dompdf\Dompdf;
87
use Drupal\advancedqueue\Entity\QueueInterface;
98
use Drupal\advancedqueue\Job;
109
use Drupal\advancedqueue\JobResult;
@@ -19,14 +18,15 @@
1918
use Drupal\Core\Mail\MailManagerInterface;
2019
use Drupal\Core\Render\Markup;
2120
use Drupal\Core\Url;
21+
use Drupal\entity_print\Plugin\EntityPrintPluginManagerInterface;
2222
use Drupal\maestro\Engine\MaestroEngine;
2323
use Drupal\maestro\Utility\TaskHandler;
2424
use Drupal\os2forms_digital_post\Helper\DigitalPostHelper;
2525
use Drupal\os2forms_digital_post\Model\Document;
2626
use Drupal\os2forms_forloeb\Exception\RuntimeException;
27+
use Drupal\os2forms_forloeb\Form\SettingsForm;
2728
use Drupal\os2forms_forloeb\Plugin\AdvancedQueue\JobType\SendMeastroNotification;
2829
use Drupal\os2forms_forloeb\Plugin\EngineTasks\MaestroWebformInheritTask;
29-
use Drupal\os2forms_forloeb\Form\SettingsForm;
3030
use Drupal\os2forms_forloeb\Plugin\WebformHandler\MaestroNotificationHandler;
3131
use Drupal\webform\WebformSubmissionInterface;
3232
use Drupal\webform\WebformSubmissionStorageInterface;
@@ -80,6 +80,7 @@ public function __construct(
8080
readonly private LoggerChannelInterface $logger,
8181
readonly private LoggerChannelInterface $submissionLogger,
8282
readonly private ModuleHandlerInterface $moduleHandler,
83+
readonly private EntityPrintPluginManagerInterface $entityPrintPluginManager,
8384
readonly private DigitalPostHelper $digitalPostHelper
8485
) {
8586
$this->config = $configFactory->get(SettingsForm::SETTINGS);
@@ -217,19 +218,19 @@ private function sendNotification(
217218
}
218219

219220
[
220-
$content,
221-
$contentType,
222-
$recipient,
223-
$subject,
224-
$taskUrl,
225-
$actionLabel,
221+
'content' => $content,
222+
'contentType' => $contentType,
223+
'recipient' => $recipient,
224+
'subject' => $subject,
225+
'taskUrl' => $taskUrl,
226+
'actionLabel' => $actionLabel,
226227
] = $this->renderNotification($submission, $handler->getHandlerId(), $notificationType, $templateTask, $maestroQueueID);
227228

228229
if ('email' === $contentType) {
229-
$this->sendNotificationEmail($recipient, $subject, $content, $submission);
230+
$this->sendNotificationEmail($recipient, $subject, $content, $submission, $notificationType);
230231
}
231232
else {
232-
$this->sendNotificationDigitalPost($recipient, $subject, $content, $taskUrl, $actionLabel, $submission);
233+
$this->sendNotificationDigitalPost($recipient, $subject, $content, $taskUrl, $actionLabel, $submission, $notificationType);
233234
}
234235
}
235236
}
@@ -273,34 +274,47 @@ private function sendNotificationEmail(
273274
string $recipient,
274275
string $subject,
275276
string $body,
276-
WebformSubmissionInterface $submission
277+
WebformSubmissionInterface $submission,
278+
string $notificationType
277279
): void {
278-
$message = [
279-
'subject' => $subject,
280-
'body' => $body,
281-
'html' => TRUE,
282-
];
280+
try {
281+
$message = [
282+
'subject' => $subject,
283+
'body' => $body,
284+
'html' => TRUE,
285+
];
283286

284-
$langcode = $this->languageManager->getCurrentLanguage()->getId();
287+
$langcode = $this->languageManager->getCurrentLanguage()->getId();
285288

286-
$result = $this->mailManager->mail(
287-
'os2forms_forloeb',
288-
'notification',
289-
$recipient,
290-
$langcode,
291-
$message
292-
);
289+
$result = $this->mailManager->mail(
290+
'os2forms_forloeb',
291+
'notification',
292+
$recipient,
293+
$langcode,
294+
$message
295+
);
293296

294-
if (!$result['result']) {
295-
throw new RuntimeException(sprintf('Error sending notification email to %s', $recipient));
296-
}
297+
if (!$result['result']) {
298+
throw new RuntimeException(sprintf('Error sending notification (%s) email to %s', $notificationType, $recipient));
299+
}
297300

298-
$this->notice('Notification email sent to @recipient', [
299-
'webform_submission' => $submission,
300-
'@recipient' => $recipient,
301-
'handler_id' => 'os2forms_forloeb',
302-
'operation' => 'notification sent',
303-
]);
301+
$this->notice('Email notification (@type) sent to @recipient', [
302+
'@type' => $notificationType,
303+
'webform_submission' => $submission,
304+
'@recipient' => $recipient,
305+
'handler_id' => 'os2forms_forloeb',
306+
'operation' => 'notification sent',
307+
]);
308+
}
309+
catch (\Exception $exception) {
310+
$this->error('Error sending email notification (@type): @message', [
311+
'@type' => $notificationType,
312+
'@message' => $exception->getMessage(),
313+
'webform_submission' => $submission,
314+
'handler_id' => 'os2forms_forloeb',
315+
'operation' => 'failed sending notification',
316+
]);
317+
}
304318
}
305319

306320
/**
@@ -312,7 +326,8 @@ private function sendNotificationDigitalPost(
312326
string $content,
313327
string $taskUrl,
314328
string $actionLabel,
315-
WebformSubmissionInterface $submission
329+
WebformSubmissionInterface $submission,
330+
string $notificationType
316331
): void {
317332
if (!$this->moduleHandler->moduleExists('os2forms_digital_post')) {
318333
throw new RuntimeException('Cannot send digital post. Module os2forms_digital_post not installed.');
@@ -349,14 +364,16 @@ private function sendNotificationDigitalPost(
349364
$submission
350365
);
351366

352-
$this->notice('Digital post sent', [
367+
$this->notice('Digital post notification sent (@type)', [
368+
'@type' => $notificationType,
353369
'webform_submission' => $submission,
354370
'handler_id' => 'os2forms_forloeb',
355371
'operation' => 'notification sent',
356372
]);
357373
}
358374
catch (\Exception $exception) {
359-
$this->error('Error sending digital post: @message', [
375+
$this->error('Error sending digital post notification (@type): @message', [
376+
'@type' => $notificationType,
360377
'@message' => $exception->getMessage(),
361378
'webform_submission' => $submission,
362379
'handler_id' => 'os2forms_forloeb',
@@ -383,13 +400,13 @@ private function sendNotificationDigitalPost(
383400
* on the recipient.
384401
*
385402
* @return array
386-
* The rendered notification as a list
387-
* - Content
388-
* - Content type
389-
* - Recipient
390-
* - Subject
391-
* - Task URL (for digital post)
392-
* - Action label (for digital post)
403+
* The rendered notification with keys
404+
* - content
405+
* - contentType
406+
* - recipient
407+
* - subject
408+
* - taskUrl (for digital post)
409+
* - actionLabel (for digital post)
393410
*/
394411
public function renderNotification(WebformSubmissionInterface $submission, string $handlerId, string $notificationType, array $templateTask, int $maestroQueueID, string $contentType = NULL): array {
395412
$handler = $submission->getWebform()->getHandler($handlerId);
@@ -463,29 +480,30 @@ public function renderNotification(WebformSubmissionInterface $submission, strin
463480

464481
switch ($contentType) {
465482
case 'email':
466-
$content = $this->buildHtml($contentType, $subject, $content, $taskUrl, $actionLabel, $submission);
483+
$content = $this->renderHtml($contentType, $subject, $content, $taskUrl, $actionLabel, $submission);
467484
break;
468485

469486
case 'pdf':
470-
$pdfContent = $this->buildHtml($contentType, $subject, $content, $taskUrl, $actionLabel, $submission);
471-
$dompdf = new Dompdf();
472-
$dompdf->loadHtml($pdfContent);
473-
$dompdf->render();
487+
$pdfContent = $this->renderHtml($contentType, $subject, $content, $taskUrl, $actionLabel, $submission);
474488

475-
$content = $dompdf->output();
489+
// Get dompdf plugin from entity_print module.
490+
/** @var \Drupal\entity_print\Plugin\EntityPrint\PrintEngine\PdfEngineBase $printer */
491+
$printer = $this->entityPrintPluginManager->createInstance('dompdf');
492+
$printer->addPage($pdfContent);
493+
$content = $printer->getBlob();
476494
break;
477495

478496
default:
479497
throw new RuntimeException(sprintf('Invalid content type: %s', $contentType));
480498
}
481499

482500
return [
483-
$content,
484-
$contentType,
485-
$recipient,
486-
$subject,
487-
$taskUrl,
488-
$actionLabel,
501+
'content' => $content,
502+
'contentType' => $contentType,
503+
'recipient' => $recipient,
504+
'subject' => $subject,
505+
'taskUrl' => $taskUrl,
506+
'actionLabel' => $actionLabel,
489507
];
490508
}
491509

@@ -495,27 +513,35 @@ public function renderNotification(WebformSubmissionInterface $submission, strin
495513
/**
496514
* Build HTML.
497515
*/
498-
private function buildHtml(
516+
private function renderHtml(
499517
string $type,
500518
string $subject,
501519
array $content,
502520
string $taskUrl,
503521
string $actionLabel,
504522
WebformSubmissionInterface $submission
505523
): string|MarkupInterface {
506-
// Render body as HTML.
507-
$theme = sprintf('os2forms_forloeb_notification_message_%s_html', $type);
524+
$template = $this->config->get('templates')['notification_' . $type] ?? NULL;
525+
if (file_exists($template)) {
526+
$template = file_get_contents($template) ?: NULL;
527+
}
528+
if (NULL === $template) {
529+
$template = 'Missing or invalid template';
530+
}
508531

509532
$build = [
510-
'#theme' => $theme,
511-
'#message' => [
512-
'subject' => $subject,
513-
'content' => $content,
533+
'#type' => 'inline_template',
534+
'#template' => $template,
535+
'#context' => [
536+
'message' => [
537+
'subject' => $subject,
538+
'content' => $content,
539+
],
540+
'task_url' => $taskUrl,
541+
'action_label' => $actionLabel,
542+
'webform_submission' => $submission,
543+
'handler' => $this,
514544
],
515-
'#task_url' => $taskUrl,
516-
'#action_label' => $actionLabel,
517-
'#webform_submission' => $submission,
518-
'#handler' => $this,
519545
];
520546

521547
return Markup::create(trim((string) $this->webformThemeManager->renderPlain($build)));

web/sites/default/modules/os2forms_forloeb/src/Plugin/WebformHandler/MaestroNotificationHandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function getEnabledNotifications(): array {
273273
MaestroHelper::OS2FORMS_FORLOEB_NOTIFICATION_ESCALATION,
274274
] as $notificationType) {
275275
if ($this->configuration[self::NOTIFICATION][$notificationType][self::NOTIFICATION_ENABLE] ?? FALSE) {
276-
$enabledNotificationTypes[] = $notificationType;
276+
$enabledNotificationTypes[$notificationType] = $notificationType;
277277
}
278278
}
279279

@@ -284,7 +284,7 @@ public function getEnabledNotifications(): array {
284284
* Check if a notification type is enabled.
285285
*/
286286
public function isNotificationEnabled(string $notificationType): bool {
287-
return in_array($notificationType, $this->getEnabledNotifications(), TRUE);
287+
return isset($this->getEnabledNotifications()[$notificationType]);
288288
}
289289

290290
}

0 commit comments

Comments
 (0)