4
4
5
5
use DigitalPost \MeMo \Action ;
6
6
use DigitalPost \MeMo \EntryPoint ;
7
- use Dompdf \Dompdf ;
8
7
use Drupal \advancedqueue \Entity \QueueInterface ;
9
8
use Drupal \advancedqueue \Job ;
10
9
use Drupal \advancedqueue \JobResult ;
19
18
use Drupal \Core \Mail \MailManagerInterface ;
20
19
use Drupal \Core \Render \Markup ;
21
20
use Drupal \Core \Url ;
21
+ use Drupal \entity_print \Plugin \EntityPrintPluginManagerInterface ;
22
22
use Drupal \maestro \Engine \MaestroEngine ;
23
23
use Drupal \maestro \Utility \TaskHandler ;
24
24
use Drupal \os2forms_digital_post \Helper \DigitalPostHelper ;
25
25
use Drupal \os2forms_digital_post \Model \Document ;
26
26
use Drupal \os2forms_forloeb \Exception \RuntimeException ;
27
+ use Drupal \os2forms_forloeb \Form \SettingsForm ;
27
28
use Drupal \os2forms_forloeb \Plugin \AdvancedQueue \JobType \SendMeastroNotification ;
28
29
use Drupal \os2forms_forloeb \Plugin \EngineTasks \MaestroWebformInheritTask ;
29
- use Drupal \os2forms_forloeb \Form \SettingsForm ;
30
30
use Drupal \os2forms_forloeb \Plugin \WebformHandler \MaestroNotificationHandler ;
31
31
use Drupal \webform \WebformSubmissionInterface ;
32
32
use Drupal \webform \WebformSubmissionStorageInterface ;
@@ -80,6 +80,7 @@ public function __construct(
80
80
readonly private LoggerChannelInterface $ logger ,
81
81
readonly private LoggerChannelInterface $ submissionLogger ,
82
82
readonly private ModuleHandlerInterface $ moduleHandler ,
83
+ readonly private EntityPrintPluginManagerInterface $ entityPrintPluginManager ,
83
84
readonly private DigitalPostHelper $ digitalPostHelper
84
85
) {
85
86
$ this ->config = $ configFactory ->get (SettingsForm::SETTINGS );
@@ -217,19 +218,19 @@ private function sendNotification(
217
218
}
218
219
219
220
[
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 ,
226
227
] = $ this ->renderNotification ($ submission , $ handler ->getHandlerId (), $ notificationType , $ templateTask , $ maestroQueueID );
227
228
228
229
if ('email ' === $ contentType ) {
229
- $ this ->sendNotificationEmail ($ recipient , $ subject , $ content , $ submission );
230
+ $ this ->sendNotificationEmail ($ recipient , $ subject , $ content , $ submission, $ notificationType );
230
231
}
231
232
else {
232
- $ this ->sendNotificationDigitalPost ($ recipient , $ subject , $ content , $ taskUrl , $ actionLabel , $ submission );
233
+ $ this ->sendNotificationDigitalPost ($ recipient , $ subject , $ content , $ taskUrl , $ actionLabel , $ submission, $ notificationType );
233
234
}
234
235
}
235
236
}
@@ -273,34 +274,47 @@ private function sendNotificationEmail(
273
274
string $ recipient ,
274
275
string $ subject ,
275
276
string $ body ,
276
- WebformSubmissionInterface $ submission
277
+ WebformSubmissionInterface $ submission ,
278
+ string $ notificationType
277
279
): 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
+ ];
283
286
284
- $ langcode = $ this ->languageManager ->getCurrentLanguage ()->getId ();
287
+ $ langcode = $ this ->languageManager ->getCurrentLanguage ()->getId ();
285
288
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
+ );
293
296
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
+ }
297
300
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
+ }
304
318
}
305
319
306
320
/**
@@ -312,7 +326,8 @@ private function sendNotificationDigitalPost(
312
326
string $ content ,
313
327
string $ taskUrl ,
314
328
string $ actionLabel ,
315
- WebformSubmissionInterface $ submission
329
+ WebformSubmissionInterface $ submission ,
330
+ string $ notificationType
316
331
): void {
317
332
if (!$ this ->moduleHandler ->moduleExists ('os2forms_digital_post ' )) {
318
333
throw new RuntimeException ('Cannot send digital post. Module os2forms_digital_post not installed. ' );
@@ -349,14 +364,16 @@ private function sendNotificationDigitalPost(
349
364
$ submission
350
365
);
351
366
352
- $ this ->notice ('Digital post sent ' , [
367
+ $ this ->notice ('Digital post notification sent (@type) ' , [
368
+ '@type ' => $ notificationType ,
353
369
'webform_submission ' => $ submission ,
354
370
'handler_id ' => 'os2forms_forloeb ' ,
355
371
'operation ' => 'notification sent ' ,
356
372
]);
357
373
}
358
374
catch (\Exception $ exception ) {
359
- $ this ->error ('Error sending digital post: @message ' , [
375
+ $ this ->error ('Error sending digital post notification (@type): @message ' , [
376
+ '@type ' => $ notificationType ,
360
377
'@message ' => $ exception ->getMessage (),
361
378
'webform_submission ' => $ submission ,
362
379
'handler_id ' => 'os2forms_forloeb ' ,
@@ -383,13 +400,13 @@ private function sendNotificationDigitalPost(
383
400
* on the recipient.
384
401
*
385
402
* @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)
393
410
*/
394
411
public function renderNotification (WebformSubmissionInterface $ submission , string $ handlerId , string $ notificationType , array $ templateTask , int $ maestroQueueID , string $ contentType = NULL ): array {
395
412
$ handler = $ submission ->getWebform ()->getHandler ($ handlerId );
@@ -463,29 +480,30 @@ public function renderNotification(WebformSubmissionInterface $submission, strin
463
480
464
481
switch ($ contentType ) {
465
482
case 'email ' :
466
- $ content = $ this ->buildHtml ($ contentType , $ subject , $ content , $ taskUrl , $ actionLabel , $ submission );
483
+ $ content = $ this ->renderHtml ($ contentType , $ subject , $ content , $ taskUrl , $ actionLabel , $ submission );
467
484
break ;
468
485
469
486
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 );
474
488
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 ();
476
494
break ;
477
495
478
496
default :
479
497
throw new RuntimeException (sprintf ('Invalid content type: %s ' , $ contentType ));
480
498
}
481
499
482
500
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 ,
489
507
];
490
508
}
491
509
@@ -495,27 +513,35 @@ public function renderNotification(WebformSubmissionInterface $submission, strin
495
513
/**
496
514
* Build HTML.
497
515
*/
498
- private function buildHtml (
516
+ private function renderHtml (
499
517
string $ type ,
500
518
string $ subject ,
501
519
array $ content ,
502
520
string $ taskUrl ,
503
521
string $ actionLabel ,
504
522
WebformSubmissionInterface $ submission
505
523
): 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
+ }
508
531
509
532
$ 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 ,
514
544
],
515
- '#task_url ' => $ taskUrl ,
516
- '#action_label ' => $ actionLabel ,
517
- '#webform_submission ' => $ submission ,
518
- '#handler ' => $ this ,
519
545
];
520
546
521
547
return Markup::create (trim ((string ) $ this ->webformThemeManager ->renderPlain ($ build )));
0 commit comments