Skip to content

Commit 23b9169

Browse files
authored
Merge pull request #95 from itk-dev/feature/1039-add-base-url-variable-to-maestro-notification-twig
#1039: Added base_url variable to maestro notification twig
2 parents 4999654 + 9da9184 commit 23b9169

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ before starting to add changes. Use example [placed in the end of the page](#exa
1414
- Adding Lat and Long fetching to DataAddress
1515
- [#84](https://github.com/OS2Forms/os2forms/pull/84)
1616
Added digital post test command.
17+
- [#95](https://github.com/OS2Forms/os2forms/pull/95)
18+
- Added `base_url` variable to twig templates.
19+
- Handled tokens in Maestro notification html.
1720
- [#92](https://github.com/OS2Forms/os2forms/pull/92)
1821
Allow denying address protected citizen from webform.
1922
- [#96](https://github.com/OS2Forms/os2forms/pull/96)

modules/os2forms_attachment/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,11 @@ To add custom headers/footer ```admin/structure/webform/config/os2forms_attachme
1313
To specify headers/footers that will override the default ones on a global level (**Third party settings** -> **Entity print** section): ```admin/structure/webform/config```
1414

1515
To specify headers/footers that will override the default ones on a form level (**Third party settings** -> **Entity print** section): ```/admin/structure/webform/manage/[webform]/settings```
16+
17+
# Overwriting templates
18+
19+
With some setups it might be necessary to overwrite templates
20+
in order to access stylesheets or images.
21+
22+
See [templates](modules/os2forms_forloeb/README.md#templates)
23+
for more details on how to do this.

modules/os2forms_attachment/os2forms_attachment.module

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
99
use Drupal\Core\Entity\EntityInterface;
1010
use Drupal\Core\Form\FormStateInterface;
11+
use Drupal\Core\Site\Settings;
1112
use Drupal\os2forms_attachment\Entity\AttachmentComponent;
1213

1314
/**
@@ -317,3 +318,12 @@ function os2forms_attachment_module_implements_alter(&$implementations, $hook) {
317318
}
318319

319320
}
321+
322+
/**
323+
* Implements hook_preprocess().
324+
*
325+
* Add 'base_url' variable to be used by templates.
326+
*/
327+
function os2forms_attachment_preprocess(&$vars) {
328+
$vars['base_url'] = Settings::get('base_url');
329+
}

modules/os2forms_forloeb/README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ You can also install the module by using Drush:
1919
Maestro 3.1 adds a `hook_webform_submission_form_alter` hook which we utilize to
2020
send assignment, reminder and escalation notifications by adding a *Maestro
2121
notification* handler to a form that spawns a Maestro workflow or assigns a
22-
task. If the notification recipient is identified by an an email address, the
22+
task. If the notification recipient is identified by an email address, the
2323
notification is sent as an email, and if the identifier is a Danish CPR number,
2424
the notifications is sent as digital post.
2525

@@ -44,3 +44,22 @@ must be processed asynchronously. Specify the queue handling notification jobs.
4444
#### Templates
4545

4646
Define templates for emails and digital post (PDF).
47+
48+
49+
To reference assets, e.g. stylesheet or images, in your templates,
50+
you can use the `base_url` Twig variable to get the base URL:
51+
52+
```html
53+
<link rel="stylesheet" href="{{ base_url }}/sites/default/templates/notification.html.twig
54+
```
55+
56+
The value of `base_url` is defined in settings.local.php:
57+
58+
```php
59+
/**
60+
* Base url.
61+
*
62+
* Used to specify full URL to stylesheets in templates.
63+
*/
64+
$settings['base_url'] = 'http://nginx:8080';
65+
```

modules/os2forms_forloeb/src/MaestroHelper.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Drupal\Core\Logger\LoggerChannelInterface;
1818
use Drupal\Core\Mail\MailManagerInterface;
1919
use Drupal\Core\Render\Markup;
20+
use Drupal\Core\Site\Settings;
2021
use Drupal\Core\Url;
2122
use Drupal\entity_print\Plugin\EntityPrintPluginManagerInterface;
2223
use Drupal\maestro\Engine\MaestroEngine;
@@ -505,7 +506,7 @@ public function renderNotification(WebformSubmissionInterface $submission, strin
505506
$processValue = static function (string $value) use ($taskUrl) {
506507
// Replace href="[maestro:task-url]" with href="«$taskUrl»".
507508
$value = preg_replace('/href\s*=\s*["\']\[maestro:task-url\]["\']/', sprintf('href="%s"', htmlspecialchars($taskUrl)), $value);
508-
$value = preg_replace('/\[(maestro:[^]]+)\]/', '&#91;\1&#93;', $value);
509+
$value = preg_replace('/\[(maestro:[^]]+)\]/', '(\1)', $value);
509510

510511
return $value;
511512
};
@@ -522,12 +523,7 @@ public function renderNotification(WebformSubmissionInterface $submission, strin
522523

523524
$content = $notificationSetting[MaestroNotificationHandler::NOTIFICATION_CONTENT];
524525
if (isset($content['value'])) {
525-
// Process tokens in content.
526-
$content['value'] = $this->tokenManager->replace(
527-
$processValue($content['value']),
528-
$submission,
529-
$maestroTokenData
530-
);
526+
$content['value'] = $processValue($content['value']);
531527
}
532528

533529
$actionLabel = $this->tokenManager->replace($notificationSetting[MaestroNotificationHandler::NOTIFICATION_ACTION_LABEL], $submission);
@@ -538,11 +534,11 @@ public function renderNotification(WebformSubmissionInterface $submission, strin
538534

539535
switch ($contentType) {
540536
case 'email':
541-
$content = $this->renderHtml($contentType, $subject, $content, $taskUrl, $actionLabel, $submission);
537+
$content = $this->renderHtml($contentType, $subject, $content, $taskUrl, $actionLabel, $submission, $maestroTokenData);
542538
break;
543539

544540
case 'pdf':
545-
$pdfContent = $this->renderHtml($contentType, $subject, $content, $taskUrl, $actionLabel, $submission);
541+
$pdfContent = $this->renderHtml($contentType, $subject, $content, $taskUrl, $actionLabel, $submission, $maestroTokenData);
546542

547543
// Get dompdf plugin from entity_print module.
548544
/** @var \Drupal\entity_print\Plugin\EntityPrint\PrintEngine\PdfEngineBase $printer */
@@ -583,6 +579,8 @@ public function renderNotification(WebformSubmissionInterface $submission, strin
583579
* The action label.
584580
* @param \Drupal\webform\WebformSubmissionInterface $submission
585581
* The webform submission.
582+
* @param array $maestroTokenData
583+
* The Maestro token data.
586584
*
587585
* @return string|MarkupInterface
588586
* The rendered content.
@@ -594,6 +592,7 @@ private function renderHtml(
594592
string $taskUrl,
595593
string $actionLabel,
596594
WebformSubmissionInterface $submission,
595+
array $maestroTokenData,
597596
): string|MarkupInterface {
598597
$template = $this->config->get('templates')['notification_' . $type] ?? NULL;
599598
if (file_exists($template)) {
@@ -615,10 +614,17 @@ private function renderHtml(
615614
'action_label' => $actionLabel,
616615
'webform_submission' => $submission,
617616
'handler' => $this,
617+
'base_url' => Settings::get('base_url'),
618618
],
619619
];
620620

621-
return Markup::create(trim((string) $this->webformThemeManager->renderPlain($build)));
621+
$html = trim((string) $this->webformThemeManager->renderPlain($build));
622+
623+
return Markup::create($this->tokenManager->replace(
624+
$html,
625+
$submission,
626+
$maestroTokenData
627+
));
622628
}
623629

624630
/**

0 commit comments

Comments
 (0)