From b328642c28dfb9a46e00687033158fb62489cf36 Mon Sep 17 00:00:00 2001 From: Oleksandr Horbatiuk Date: Sun, 3 Sep 2023 08:45:55 +0300 Subject: [PATCH] Issue #262: Set the E-mail notification sender address based on the username of the default transporter --- modules/features/d8_mail/d8_mail.module | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/modules/features/d8_mail/d8_mail.module b/modules/features/d8_mail/d8_mail.module index 06ddd6dd..5f229081 100644 --- a/modules/features/d8_mail/d8_mail.module +++ b/modules/features/d8_mail/d8_mail.module @@ -5,6 +5,7 @@ * Provides a wrapper for the Drupal Symfony Mailer module. */ +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; @@ -35,6 +36,29 @@ function d8_mail_help($route_name, RouteMatchInterface $route_match) { return $output; } +/** + * Implements hook_ENTITY_TYPE_update(). + */ +function d8_mail_mailer_transport_update(EntityInterface $entity) { + /** @var \Drupal\symfony_mailer\MailerTransportInterface $mailer */ + /** @var \Drupal\Core\Plugin\ObjectWithPluginCollectionInterface $mailer */ + $mailer = $entity; + + if ( + $mailer->isDefault() && + !empty($address = $mailer->getPluginCollections()['configuration']['user']) + ) { + /** @var \Drupal\Component\Utility\EmailValidatorInterface $validator */ + $validator = \Drupal::service('email.validator'); + + if ($validator->isValid($address)) { + \Drupal::configFactory()->getEditable('system.site') + ->set('mail', $address) + ->save(); + } + } +} + /** * Implements hook_preprocess_HOOK(). */