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

LOOP-1117: Added headings to mail notification messages #213

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#type' => 'textarea',
'#rows' => 20,
'#title' => $this->t('Body template for mail notifications'),
'#description' => $this->t('Use [os2loop_mail_notifications:messages] to insert the actual list of notification messages.'),
'#description' => $this->t('Use "Browse available tokens" to see token related to the actual notification messages.'),
'#required' => TRUE,
'#default_value' => $config->get('template_body'),
'#token_insert' => TRUE,
Expand All @@ -104,8 +104,9 @@ public function buildForm(array $form, FormStateInterface $form_state) {
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$template = $form_state->getValue('template_body');
if (FALSE === strpos($template, '[os2loop_mail_notifications:messages]')) {
$form_state->setErrorByName('template_body', $this->t('Please insert [os2loop_mail_notifications:messages] in body template.'));
if (FALSE === strpos($template, '[os2loop_mail_notifications:messages]')
&& FALSE === strpos($template, '[os2loop_mail_notifications:messages_with_headings]')) {
$form_state->setErrorByName('template_body', $this->t('Please insert either [os2loop_mail_notifications:messages] or [os2loop_mail_notifications:messages_with_headings] in body template.'));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function mail($key, &$message, $params) {
'os2loop_mail_notifications' => [
// Prevent html escaping by converting to markup.
'messages' => Markup::create($params['messages']),
'messages_with_headings' => Markup::create($params['messages_with_headings']),
],
];
$message['subject'] = $this->renderTemplate($subject_template, $data);
Expand All @@ -90,6 +91,34 @@ public function sendNotification(User $user, array $groupedMessages) {
$sections[$type] = $section;
$params[$type] = $section;
}

// Group messages under headings.
//
// A message on the form
//
// Something new: <a href="…">New stuff</a>.
//
// will be put under the heading "Something new" (colon and space are
// removed) as <a href="…">New stuff</a> (only a element is used).
$messageSections = [];
foreach ($groupedMessages as $messages) {
foreach ($messages as $message) {
$text = $message->getText($lang_code)[0] ?? NULL;
// Use text before a element as heading and keep only the a element as
// text.
if (NULL !== $text
&& preg_match('@^(?P<heading>[^<]+?)(:\s*)?(?P<content><a.+</a>)@', $text, $matches)) {
[$heading, $content] = [$matches['heading'], $matches['content']];
$messageSections[$heading][] = $content;
}
}
}
$messagesWithHeadings = '';
foreach ($messageSections as $heading => $content) {
$messagesWithHeadings .= $heading . PHP_EOL . PHP_EOL . '* ' . implode(PHP_EOL . '* ', $content) . PHP_EOL . PHP_EOL;
}
$params['messages_with_headings'] = $messagesWithHeadings;

$sections = array_filter($sections);
$params['messages'] = implode(PHP_EOL . PHP_EOL, $sections);
$params['user'] = $user;
Expand Down Expand Up @@ -144,6 +173,10 @@ public function tokenInfo() {
'name' => $this->t('The messages'),
'description' => $this->t('The messages.'),
],
'messages_with_headings' => [
'name' => $this->t('The messages with headings'),
'description' => $this->t('The messages in sections with headings.'),
],
],
],
];
Expand Down