diff --git a/comment_notify.module b/comment_notify.module index db82a50..4811541 100644 --- a/comment_notify.module +++ b/comment_notify.module @@ -523,11 +523,52 @@ function _comment_notify_mailalert(CommentInterface $comment) { ->notice('Notified: @user_mail', $args); } } + + // Send notifications to all users with permission All comments. + comment_notify_all_comments_send($comment); + // Record that a notification was sent for this comment so that // notifications aren't sent again if the comment is later edited. comment_notify_mark_comment_as_notified($comment); } +/** + * Sends notifications to all user with permission All comments. + * + * @param CommentInterface $comment + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ +function comment_notify_all_comments_send(CommentInterface $comment) { + $config = \Drupal::config('comment_notify.settings'); + $nid = $comment->getCommentedEntityId(); + $node = \Drupal::entityTypeManager()->getStorage('node')->load($nid); + $users = User::loadMultiple(); + $allCommentsUsers = []; + foreach ($users as $user) { + $account = User::load($user->id()); + if ($account->hasPermission('all comments') && $account->isAuthenticated()) { + $allCommentsUsers[] = $account->id(); + } + } + foreach ($allCommentsUsers as $key => $id) { + $user = User::load($id); + $mail = $user->getEmail(); + $language = $user->getPreferredLangcode(); + $message = []; + $token_data = ['comment' => $comment, 'node' => $node]; + $raw_values = $config->get('mail_templates.all_comments'); + $message['subject'] = PlainTextOutput::renderFromHtml(\Drupal::token() + ->replace($raw_values['subject'], $token_data)); + $message['body'] = \Drupal::token() + ->replace($raw_values['body'], $token_data); + \Drupal::service('plugin.manager.mail') + ->mail('comment_notify', 'comment_notify_mail', $mail, $language, $message); + } + comment_notify_mark_comment_as_notified($comment); +} + /** * Implements hook_mail(). */ diff --git a/comment_notify.permissions.yml b/comment_notify.permissions.yml index 2f5fdf1..2a89241 100644 --- a/comment_notify.permissions.yml +++ b/comment_notify.permissions.yml @@ -4,3 +4,6 @@ 'subscribe to comments': title: 'Subscribe to comment notifications' description: 'Subscribe to recieve notifications when new comments are posted.' +'all comments': + title: 'All comments' + description: 'Receive notifications for all comments.' diff --git a/config/install/comment_notify.settings.yml b/config/install/comment_notify.settings.yml index dbd4827..b003bf0 100644 --- a/config/install/comment_notify.settings.yml +++ b/config/install/comment_notify.settings.yml @@ -53,3 +53,23 @@ mail_templates: -- [site:name] team [site:url] + all_comments: + subject: '[site:name] :: new comment on post' + body: | + Hi, + + A new comment has been added on: "[node:title]" + + ---- + [comment:title] + [comment:body] + ---- + + You can view the comment at the following url + [comment:url] + + You will receive emails like this for all comments on site. You can + disable this by contacting the site administrator. + + -- [site:name] team + [site:url] diff --git a/config/schema/comment_notify.schema.yml b/config/schema/comment_notify.schema.yml index 0dd7fc9..6cfb649 100644 --- a/config/schema/comment_notify.schema.yml +++ b/config/schema/comment_notify.schema.yml @@ -57,3 +57,13 @@ comment_notify.settings: body: type: text label: Body + all_comments: + type: mapping + label: Send to all users with all comments permission + mapping: + subject: + type: label + label: Subject + body: + type: text + label: Body diff --git a/src/Form/CommentNotifySettings.php b/src/Form/CommentNotifySettings.php index cd811e5..9a75a03 100644 --- a/src/Form/CommentNotifySettings.php +++ b/src/Form/CommentNotifySettings.php @@ -288,6 +288,34 @@ class CommentNotifySettings extends ConfigFormBase { ]; } + $form['mail_templates']['all_comments'] = [ + '#type' => 'container', + '#tree' => TRUE, + ]; + + $form['mail_templates']['all_comments']['subject'] = [ + '#type' => 'textfield', + '#title' => $this->t('Default mail subject for sending out the notifications to users with all comments permission'), + '#default_value' => $config->get('mail_templates.all_comments.subject'), + '#token_types' => [ + 'comment', + 'node', + ], + '#element_validate' => ['token_element_validate'], + ]; + $form['mail_templates']['all_comments']['body'] = [ + '#type' => 'textarea', + '#title' => $this->t('Default mail text for sending out the notifications to users with all comments permission'), + '#default_value' => $config->get('mail_templates.all_comments.body'), + '#cols' => 80, + '#rows' => 15, + '#token_types' => [ + 'comment', + 'node', + ], + '#element_validate' => ['token_element_validate'], + ]; + $form['token_help'] = [ '#theme' => 'token_tree_link', '#token_types' => [ @@ -394,6 +422,16 @@ class CommentNotifySettings extends ConfigFormBase { $entity_type, 'body', ])); + $config->set('mail_templates.all_comments.subject', $form_state->getValue([ + 'mail_templates', + 'all_comments', + 'subject', + ])); + $config->set('mail_templates.all_comments.body', $form_state->getValue([ + 'mail_templates', + 'all_comments', + 'body', + ])); } $config->save(); parent::submitForm($form, $form_state);