Skip to content
Snippets Groups Projects
Commit a58db8ff authored by Saiyad Zaryab Haider's avatar Saiyad Zaryab Haider Committed by Claudiu Cristea
Browse files

Issue #3484854 by zaryab_drupal, claudiu.cristea, chhavi.sharma, herved: Fatal...

Issue #3484854 by zaryab_drupal, claudiu.cristea, chhavi.sharma, herved: Fatal error with email notification if user has no email address
parent ebeaa0a1
No related branches found
No related tags found
1 merge request!1343484854-fatal-error-with-email-notification PR with resolved
Pipeline #331722 passed
......@@ -5,6 +5,7 @@ namespace Drupal\private_message_notify\Service;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\message_notify\MessageNotifier;
......@@ -67,6 +68,13 @@ class PrivateMessageNotifier implements PrivateMessageNotifierInterface {
*/
protected $moduleHandler;
/**
* The logger service for private message notifications.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* Constructs a new PrivateMessageNotifier object.
*
......@@ -84,6 +92,8 @@ class PrivateMessageNotifier implements PrivateMessageNotifierInterface {
* The message notification service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler service.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
* The logger channel factory.
*/
public function __construct(
PrivateMessageServiceInterface $privateMessageService,
......@@ -93,6 +103,7 @@ class PrivateMessageNotifier implements PrivateMessageNotifierInterface {
EntityTypeManagerInterface $entityTypeManager,
MessageNotifier $messageNotifier,
ModuleHandlerInterface $moduleHandler,
LoggerChannelFactoryInterface $loggerFactory,
) {
$this->privateMessageService = $privateMessageService;
$this->currentUser = $currentUser;
......@@ -101,6 +112,7 @@ class PrivateMessageNotifier implements PrivateMessageNotifierInterface {
$this->entityTypeManager = $entityTypeManager;
$this->messageNotifier = $messageNotifier;
$this->moduleHandler = $moduleHandler;
$this->logger = $loggerFactory->get('private_message_notify');
}
/**
......@@ -110,28 +122,39 @@ class PrivateMessageNotifier implements PrivateMessageNotifierInterface {
$members = $this->getNotificationRecipients($message, $thread);
foreach ($members as $member) {
if ($member->id() != $this->currentUser->id()) {
// Check if the notification should be sent.
if ($this->shouldSend($member, $message, $thread)) {
// Send the notification.
// This is done through integration with the Message module, by
// creating a new Message entity.
$notification = $this->entityTypeManager
->getStorage('message')
->create(
[
'template' => 'private_message_notification',
'uid' => $member->id(),
]
);
$notification->set('field_message_private_message', $message);
$notification->set('field_message_pm_thread', $thread);
$notification->setLanguage($member->getPreferredLangcode());
$notification->save();
// Skip the current user and any member without a valid email.
if ($member->id() == $this->currentUser->id()) {
continue;
}
$this->messageNotifier->send($notification);
}
// Assuming getEmail() method exists.
$email = $member->getEmail();
if (empty($email)) {
// Log a warning if the email is missing, then skip this member.
$this->logger->warning('Notification not sent to user ID @uid due to missing email.', ['@uid' => $member->id()]);
continue;
}
// Check if the notification should be sent.
if (!$this->shouldSend($member, $message, $thread)) {
continue;
}
// Create and send the notification.
$notification = $this->entityTypeManager
->getStorage('message')
->create(
[
'template' => 'private_message_notification',
'uid' => $member->id(),
]
);
$notification->set('field_message_private_message', $message);
$notification->set('field_message_pm_thread', $thread);
$notification->setLanguage($member->getPreferredLangcode());
$notification->save();
$this->messageNotifier->send($notification);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment