public function AdminContentNotificationService::sendMail in Admin Content Notification 8.3
Send Eamil.
Parameters
Drupal\node\NodeInterface $node:
bool $is_new:
File
- src/
AdminContentNotificationService.php, line 116
Class
- AdminContentNotificationService
- AdminContentNotificationService implement helper service class.
Namespace
Drupal\admin_content_notificationCode
public function sendMail(NodeInterface $node, $is_new = FALSE) {
global $base_url;
$config = $this
->getConfigs();
$node_type = $node
->getType();
$node_type_label = $node->type->entity
->label();
// Checking if the nodetype is the one selected.
$selected_node_types = $config
->get('admin_content_notification_node_types');
if (count($selected_node_types) && in_array($node_type, $selected_node_types)) {
// Check if limiting based on node status.
$selected_node_status = $config
->get('admin_content_notification_trigger_on_node_status');
if ($selected_node_status > 0) {
$node_published = $node
->isPublished();
// Don't notify of published nodes.
if ($node_published && $selected_node_status == 2) {
return;
}
elseif (!$node_published && $selected_node_status == 1) {
return;
}
}
$user = $is_new ? $node
->getOwner() : $node
->getRevisionUser();
$user_name = $user
->getDisplayName();
$url = Url::fromUri($base_url . '/node/' . $node
->id());
$internal_link = $this->linkGenerator
->generate($this
->t('@title', [
'@title' => $node
->label(),
]), $url);
$variables = [
'@user_who_posted' => $user_name,
'@content_link' => $internal_link,
'@content_title' => $node
->label(),
'@content_type' => $node_type_label,
'@action' => $is_new ? $this
->t('posted') : $this
->t('updated'),
];
$subject = $this
->t($config
->get('admin_content_notification_email_subject'), $variables);
$body = $this
->t($config
->get('admin_content_notification_email_body'), $variables);
if ($this
->isTokenEnabled()) {
$token_service = \Drupal::token();
// Replace the token for body.
$body = $token_service
->replace($body, [
'node' => $node,
]);
$subject = $token_service
->replace($subject, [
'node' => $node,
]);
}
$admin_email = $config
->get('admin_content_notification_email');
if (empty($admin_email)) {
$roles_notify = array_keys(array_filter($config
->get('admin_content_notification_roles_notified')));
$ids = !empty($roles_notify) ? $this
->getUsersOfRoles($roles_notify) : [];
$emails = [];
if (count($ids)) {
$users = User::loadMultiple($ids);
foreach ($users as $userload) {
$emails[] = $userload
->getEmail();
}
}
$admin_email = implode(',', $emails);
}
$to = \Drupal::config('system.site')
->get('mail');
$params = [
'body' => $body,
'subject' => $subject,
'bcc' => $admin_email,
'nid' => $node
->id(),
];
// Allow to alter $admin_email
// by using hook_admin_content_notification_recipients_alter().
// @see admin_content_notification.api.php
\Drupal::moduleHandler()
->alter('admin_content_notification_recipients', $admin_email, $node);
// Allow to alter $params
// by using hook_admin_content_notification_params_alter().
// @see admin_content_notification.api.php
\Drupal::moduleHandler()
->alter('admin_content_notification_params', $params, $node);
if (strlen($admin_email) === 0) {
return;
}
$key = 'admin_content_notification_key';
$this->mailManager
->mail('admin_content_notification', $key, $to, 'en', $params, \Drupal::config('system.site')
->get('mail'), TRUE);
$this
->getLogger('admin_content_notification')
->notice($this
->t('Admin content notification sent to @emails.', [
'@emails' => $admin_email,
]));
}
}