From 69e2bdb55085ca31b8dc05609b2f6df10d7032dc Mon Sep 17 00:00:00 2001 From: Mikkel Ricky Date: Wed, 15 Sep 2021 14:00:17 +0200 Subject: [PATCH] LOOP-1117: Added headings to mail notification messages --- .../src/Form/SettingsForm.php | 7 ++-- .../src/Helper/MailHelper.php | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Form/SettingsForm.php b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Form/SettingsForm.php index ac15f4b55..a6ad3b903 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Form/SettingsForm.php +++ b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Form/SettingsForm.php @@ -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, @@ -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.')); } } diff --git a/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Helper/MailHelper.php b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Helper/MailHelper.php index e8980b8dc..7c1deff7c 100644 --- a/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Helper/MailHelper.php +++ b/web/profiles/custom/os2loop/modules/os2loop_mail_notifications/src/Helper/MailHelper.php @@ -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); @@ -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: New stuff. + // + // will be put under the heading "Something new" (colon and space are + // removed) as New stuff (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[^<]+?)(:\s*)?(?P)@', $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; @@ -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.'), + ], ], ], ];