Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1039: Added base_url variable to maestro notification twig #95

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ before starting to add changes. Use example [placed in the end of the page](#exa
- Adding Lat and Long fetching to DataAddress
- [#84](https://github.com/OS2Forms/os2forms/pull/84)
Added digital post test command.
- [#95](https://github.com/OS2Forms/os2forms/pull/95)
- Added `base_url` variable to twig templates.
- Handled tokens in Maestro notification html.

## [3.14.1] 2024-01-16

Expand Down
8 changes: 8 additions & 0 deletions modules/os2forms_attachment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ To add custom headers/footer ```admin/structure/webform/config/os2forms_attachme
To specify headers/footers that will override the default ones on a global level (**Third party settings** -> **Entity print** section): ```admin/structure/webform/config```

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```

jekuaitk marked this conversation as resolved.
Show resolved Hide resolved
# Overwriting templates

With some setups it might be necessary to overwrite templates
in order to access stylesheets or images.

See [templates](modules/os2forms_forloeb/README.md#templates)
for more details on how to do this.
10 changes: 10 additions & 0 deletions modules/os2forms_attachment/os2forms_attachment.module
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Site\Settings;
use Drupal\os2forms_attachment\Entity\AttachmentComponent;

/**
Expand Down Expand Up @@ -317,3 +318,12 @@ function os2forms_attachment_module_implements_alter(&$implementations, $hook) {
}

}

/**
* Implements hook_preprocess().
*
* Add 'base_url' variable to be used by templates.
*/
function os2forms_attachment_preprocess(&$vars) {
$vars['base_url'] = Settings::get('base_url');
jekuaitk marked this conversation as resolved.
Show resolved Hide resolved
}
21 changes: 20 additions & 1 deletion modules/os2forms_forloeb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ You can also install the module by using Drush:
Maestro 3.1 adds a `hook_webform_submission_form_alter` hook which we utilize to
send assignment, reminder and escalation notifications by adding a *Maestro
notification* handler to a form that spawns a Maestro workflow or assigns a
task. If the notification recipient is identified by an an email address, the
task. If the notification recipient is identified by an email address, the
notification is sent as an email, and if the identifier is a Danish CPR number,
the notifications is sent as digital post.

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

Define templates for emails and digital post (PDF).


To reference assets, e.g. stylesheet or images, in your templates,
you can use the `base_url` Twig variable to get the base URL:

```html
<link rel="stylesheet" href="{{ base_url }}/sites/default/templates/notification.html.twig
```

The value of `base_url` is defined in settings.local.php:

```php
/**
* Base url.
*
* Used to specify full URL to stylesheets in templates.
*/
$settings['base_url'] = 'http://nginx:8080';
```
28 changes: 17 additions & 11 deletions modules/os2forms_forloeb/src/MaestroHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\entity_print\Plugin\EntityPrintPluginManagerInterface;
use Drupal\maestro\Engine\MaestroEngine;
Expand Down Expand Up @@ -505,7 +506,7 @@ public function renderNotification(WebformSubmissionInterface $submission, strin
$processValue = static function (string $value) use ($taskUrl) {
// Replace href="[maestro:task-url]" with href="«$taskUrl»".
$value = preg_replace('/href\s*=\s*["\']\[maestro:task-url\]["\']/', sprintf('href="%s"', htmlspecialchars($taskUrl)), $value);
$value = preg_replace('/\[(maestro:[^]]+)\]/', '&#91;\1&#93;', $value);
$value = preg_replace('/\[(maestro:[^]]+)\]/', '(\1)', $value);

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

$content = $notificationSetting[MaestroNotificationHandler::NOTIFICATION_CONTENT];
if (isset($content['value'])) {
// Process tokens in content.
$content['value'] = $this->tokenManager->replace(
$processValue($content['value']),
$submission,
$maestroTokenData
);
$content['value'] = $processValue($content['value']);
}

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

switch ($contentType) {
case 'email':
$content = $this->renderHtml($contentType, $subject, $content, $taskUrl, $actionLabel, $submission);
$content = $this->renderHtml($contentType, $subject, $content, $taskUrl, $actionLabel, $submission, $maestroTokenData);
break;

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

// Get dompdf plugin from entity_print module.
/** @var \Drupal\entity_print\Plugin\EntityPrint\PrintEngine\PdfEngineBase $printer */
Expand Down Expand Up @@ -583,6 +579,8 @@ public function renderNotification(WebformSubmissionInterface $submission, strin
* The action label.
* @param \Drupal\webform\WebformSubmissionInterface $submission
* The webform submission.
* @param array $maestroTokenData
* The Maestro token data.
*
* @return string|MarkupInterface
* The rendered content.
Expand All @@ -593,7 +591,8 @@ private function renderHtml(
array $content,
string $taskUrl,
string $actionLabel,
WebformSubmissionInterface $submission
WebformSubmissionInterface $submission,
array $maestroTokenData,
): string|MarkupInterface {
$template = $this->config->get('templates')['notification_' . $type] ?? NULL;
if (file_exists($template)) {
Expand All @@ -615,10 +614,17 @@ private function renderHtml(
'action_label' => $actionLabel,
'webform_submission' => $submission,
'handler' => $this,
'base_url' => Settings::get('base_url'),
],
];

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

return Markup::create($this->tokenManager->replace(
$html,
$submission,
$maestroTokenData
));
}

/**
Expand Down
Loading