Skip to content

Commit

Permalink
Merge pull request #95 from itk-dev/feature/1039-add-base-url-variabl…
Browse files Browse the repository at this point in the history
…e-to-maestro-notification-twig

#1039: Added base_url variable to maestro notification twig
  • Loading branch information
jekuaitk committed May 3, 2024
2 parents 4999654 + 9da9184 commit 23b9169
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
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.
- [#92](https://github.com/OS2Forms/os2forms/pull/92)
Allow denying address protected citizen from webform.
- [#96](https://github.com/OS2Forms/os2forms/pull/96)
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```

# 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');
}
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';
```
26 changes: 16 additions & 10 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 @@ -594,6 +592,7 @@ private function renderHtml(
string $taskUrl,
string $actionLabel,
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

0 comments on commit 23b9169

Please sign in to comment.