Skip to content
Snippets Groups Projects
Commit 19ffaad4 authored by Paul Mrvik's avatar Paul Mrvik
Browse files

Issue #3488845 by globexplorer: As a user I want to be informend when notifications won't sent

parent 8e0c807f
No related branches found
No related tags found
No related merge requests found
services: services:
conditional_notification.helper: conditional_notification.helper:
class: Drupal\conditional_notification\Helper class: Drupal\conditional_notification\Helper
arguments: ['@entity_type.manager', '@current_user', '@config.factory', '@logger.factory', '@entity_type.bundle.info', '@entity_field.manager'] arguments: ['@entity_type.manager', '@current_user', '@config.factory', '@entity_type.bundle.info', '@entity_field.manager']
conditional_notification.issue_informer:
class: Drupal\conditional_notification\IssueInformer
arguments: ['@config.factory', '@plugin.manager.mail', '@email.validator']
conditional_notification.route_subscriber: conditional_notification.route_subscriber:
class: Drupal\conditional_notification\Routing\RouteSubscriber class: Drupal\conditional_notification\Routing\RouteSubscriber
arguments: ['@entity_type.manager', '@router.route_provider'] arguments: ['@entity_type.manager', '@router.route_provider']
......
...@@ -17,6 +17,11 @@ conditional_notification.settings: ...@@ -17,6 +17,11 @@ conditional_notification.settings:
notification_text_format: notification_text_format:
label: 'Notification text format' label: 'Notification text format'
type: string type: string
issue_informer_emails:
label: 'Notification issue informer emails'
type: text
issue_informer_subject:
label: 'Notification issue informer subject'
conditional_notification.conditional_notification.*: conditional_notification.conditional_notification.*:
type: config_entity type: config_entity
......
...@@ -9,6 +9,7 @@ use Drupal\Core\Form\FormStateInterface; ...@@ -9,6 +9,7 @@ use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\conditional_notification\Helper; use Drupal\conditional_notification\Helper;
use Drupal\conditional_notification\IssueInformer;
/** /**
* Configure Conditional Notification settings for this site. * Configure Conditional Notification settings for this site.
...@@ -22,6 +23,13 @@ final class SettingsForm extends ConfigFormBase { ...@@ -22,6 +23,13 @@ final class SettingsForm extends ConfigFormBase {
*/ */
protected $helperService; protected $helperService;
/**
* The Issue informer service.
*
* @var \Drupal\conditional_notification\IssueInformer
*/
protected $issueInformer;
/** /**
* Constructs a new SettingsForm object. * Constructs a new SettingsForm object.
* *
...@@ -29,10 +37,13 @@ final class SettingsForm extends ConfigFormBase { ...@@ -29,10 +37,13 @@ final class SettingsForm extends ConfigFormBase {
* The factory for configuration objects. * The factory for configuration objects.
* @param \Drupal\conditional_notification\Helper $helper_service * @param \Drupal\conditional_notification\Helper $helper_service
* The helper service. * The helper service.
* @param \Drupal\conditional_notification\IssueInformer $issue_informer
* The issue informer service.
*/ */
public function __construct(ConfigFactoryInterface $config_factory, Helper $helper_service) { public function __construct(ConfigFactoryInterface $config_factory, Helper $helper_service, IssueInformer $issue_informer) {
parent::__construct($config_factory); parent::__construct($config_factory);
$this->helperService = $helper_service; $this->helperService = $helper_service;
$this->issueInformer = $issue_informer;
} }
/** /**
...@@ -41,7 +52,8 @@ final class SettingsForm extends ConfigFormBase { ...@@ -41,7 +52,8 @@ final class SettingsForm extends ConfigFormBase {
public static function create(ContainerInterface $container) { public static function create(ContainerInterface $container) {
return new static( return new static(
$container->get('config.factory'), $container->get('config.factory'),
$container->get('conditional_notification.helper') $container->get('conditional_notification.helper'),
$container->get('conditional_notification.issue_informer')
); );
} }
...@@ -95,6 +107,24 @@ final class SettingsForm extends ConfigFormBase { ...@@ -95,6 +107,24 @@ final class SettingsForm extends ConfigFormBase {
'#default_value' => $notification_text_format_default, '#default_value' => $notification_text_format_default,
]; ];
$issue_informer_emails_default = $this->config('conditional_notification.settings')->get('issue_informer_emails') ?? '';
$form['issue_informer_emails'] = [
'#title' => $this->t('Notification informer emails'),
'#description' => $this->t('Get a notification when a conditional notification has not been sent. Use ; to separate the emails.'),
'#type' => 'textarea',
'#default_value' => $issue_informer_emails_default,
];
$issue_informer_subject_default = $this->config('conditional_notification.settings')->get('issue_informer_subject') ?? '';
$form['issue_informer_subject'] = [
'#title' => $this->t('Notification informer subject'),
'#description' => $this->t('Define your unique subject for those kind of emails.'),
'#type' => 'textfield',
'#default_value' => $issue_informer_subject_default,
];
return parent::buildForm($form, $form_state); return parent::buildForm($form, $form_state);
} }
...@@ -102,16 +132,19 @@ final class SettingsForm extends ConfigFormBase { ...@@ -102,16 +132,19 @@ final class SettingsForm extends ConfigFormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function validateForm(array &$form, FormStateInterface $form_state): void { public function validateForm(array &$form, FormStateInterface $form_state): void {
// @todo Validate the form here. if (!empty($form_state->getValue('issue_informer_emails'))) {
// Example: $issue_informer_emails = trim($form_state->getValue('issue_informer_emails'));
// @code $emails = explode(';', $issue_informer_emails);
// if ($form_state->getValue('example') === 'wrong') { foreach ($emails as $email) {
// $form_state->setErrorByName( if (!$this->issueInformer->isEmailValid($email)) {
// 'message', $form_state->setErrorByName(
// $this->t('The value is not correct.'), 'issue_informer_emails',
// ); $this->t('Unvalid emails detected! Did you use ; as delimiter to separate the email addresses?'),
// } );
// @endcode }
}
}
parent::validateForm($form, $form_state); parent::validateForm($form, $form_state);
} }
...@@ -122,6 +155,8 @@ final class SettingsForm extends ConfigFormBase { ...@@ -122,6 +155,8 @@ final class SettingsForm extends ConfigFormBase {
$this->config('conditional_notification.settings') $this->config('conditional_notification.settings')
->set('supported_entities', $form_state->getValue('supported_entities')) ->set('supported_entities', $form_state->getValue('supported_entities'))
->set('notification_text_format', $form_state->getValue('notification_text_format')) ->set('notification_text_format', $form_state->getValue('notification_text_format'))
->set('issue_informer_emails', $form_state->getValue('issue_informer_emails'))
->set('issue_informer_subject', $form_state->getValue('issue_informer_subject'))
->save(); ->save();
parent::submitForm($form, $form_state); parent::submitForm($form, $form_state);
} }
......
...@@ -8,14 +8,16 @@ use Drupal\Core\Config\ConfigFactoryInterface; ...@@ -8,14 +8,16 @@ use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Entity\ContentEntityType; use Drupal\Core\Entity\ContentEntityType;
use Drupal\conditional_notification\ConditionalNotificationInterface; use Drupal\conditional_notification\ConditionalNotificationInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Component\Utility\EmailValidatorInterface;
use Drupal\Core\Utility\Token;
use Drupal\Core\Logger\LoggerChannelTrait;
/** /**
...@@ -25,6 +27,8 @@ final class Helper { ...@@ -25,6 +27,8 @@ final class Helper {
use StringTranslationTrait; use StringTranslationTrait;
use LoggerChannelTrait;
/** /**
* Constructs a Helper object. * Constructs a Helper object.
*/ */
...@@ -32,7 +36,6 @@ final class Helper { ...@@ -32,7 +36,6 @@ final class Helper {
private readonly EntityTypeManagerInterface $entityTypeManager, private readonly EntityTypeManagerInterface $entityTypeManager,
private readonly AccountProxyInterface $currentUser, private readonly AccountProxyInterface $currentUser,
private readonly ConfigFactoryInterface $configFactory, private readonly ConfigFactoryInterface $configFactory,
private readonly LoggerChannelFactoryInterface $loggerFactory,
private readonly EntityTypeBundleInfoInterface $entityTypeBundleInfo, private readonly EntityTypeBundleInfoInterface $entityTypeBundleInfo,
private readonly EntityFieldManagerInterface $entityFieldManager, private readonly EntityFieldManagerInterface $entityFieldManager,
) {} ) {}
......
<?php
declare(strict_types=1);
namespace Drupal\conditional_notification;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Entity\ContentEntityType;
use Drupal\conditional_notification\ConditionalNotificationInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Component\Utility\EmailValidatorInterface;
use Drupal\Core\Utility\Token;
use Drupal\Core\Logger\LoggerChannelTrait;
/**
* @todo Add class description.
*/
final class IssueInformer {
use StringTranslationTrait;
use LoggerChannelTrait;
/**
* Constructs a Helper object.
*/
public function __construct(
private readonly ConfigFactoryInterface $configFactory,
private readonly MailManagerInterface $mailManager,
private readonly EmailValidatorInterface $emailValidator,
) {}
/**
* Check for a valid email
*
* @param string $email
* The email to check
* @return boolean
* TRUE | FALSE
*/
public function isEmailValid(string $email): bool {
return $this->emailValidator->isValid($email);
}
function sendIssueInformerMail(array $debug) {
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
$validated_emails = FALSE;
$config = $this->configFactory->get('conditional_notification.settings');
if ($issue_informer_emails = $config->get('issue_informer_emails')) {
// Get emails from field
$emails = explode(';', trim($issue_informer_emails));
// Get subject
if ($subject = $config->get('issue_informer_subject')) {
$email_subject = $subject;
}
else {
$email_subject = t('Issue informer Conditional Notifcations!');
}
$context = [
'subject' => $email_subject,
'message' => json_encode($debug),
];
foreach ($emails as $email) {
// Sending Email
$delivered = $this->mailManager->mail('system', 'action_send_email', $email, $langcode, [
'context' => $context
]);
}
if (!$delivered) {
$this->getLogger('conditional_notification')->warning('A issue informer message was not delivered!');
}
else {
$this->getLogger('conditional_notification')->notice('A issue informer message was successfully delivered!');
}
}
}
}
...@@ -120,10 +120,12 @@ final class NotificationQueue extends QueueWorkerBase implements ContainerFactor ...@@ -120,10 +120,12 @@ final class NotificationQueue extends QueueWorkerBase implements ContainerFactor
} }
} }
else { else {
\Drupal::service('conditional_notification.issue_informer')->sendIssueInformerMail($data);
$this->getLogger('conditional_notification')->warning('No users found to send notifications to!'); $this->getLogger('conditional_notification')->warning('No users found to send notifications to!');
} }
} }
else { else {
\Drupal::service('conditional_notification.issue_informer')->sendIssueInformerMail($data);
$debug = json_encode($data); $debug = json_encode($data);
$this->getLogger('conditional_notification') $this->getLogger('conditional_notification')
->warning('No recipients found. Check the following plugin: @context. Debug info: @debug', [ ->warning('No recipients found. Check the following plugin: @context. Debug info: @debug', [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment