content_moderation_notifications.module in Content Moderation Notifications 8
File
content_moderation_notifications.module
View source
<?php
function content_moderation_notifications_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
$mailManager = \Drupal::service('plugin.manager.mail');
$langcode = \Drupal::currentUser()
->getPreferredLangcode();
$moderation_info = Drupal::service('content_moderation.moderation_information');
$is_moderated = $moderation_info
->isModeratedEntity($entity);
if ($is_moderated) {
$previous_state = $entity->original->moderation_state->target_id;
$current_state = $entity->moderation_state->target_id;
$transitions = Drupal::service('entity_type.manager')
->getStorage('moderation_state_transition')
->loadMultiple();
foreach ($transitions as $key => $transition) {
if ($transition
->getFromState() == $previous_state && $transition
->getToState() == $current_state) {
$query = \Drupal::entityQuery('content_moderation_notification')
->condition('transitions', $transition
->id(), 'IN');
$notification_ids = $query
->execute();
$notifications = Drupal::service('entity_type.manager')
->getStorage('content_moderation_notification')
->loadMultiple($notification_ids);
foreach ($notifications as $notification) {
$params['subject'] = $notification->subject;
$params['message'] = check_markup($notification->body['value'], $notification->body['format']);
$to = array();
if ($notification->author) {
$to[] = $entity
->getOwner()->mail->value;
}
$roles = array_keys(array_filter($notification->roles));
foreach ($roles as $role) {
$role_users = \Drupal::service('entity_type.manager')
->getStorage('user')
->loadByProperties([
'roles' => $role,
]);
foreach ($role_users as $role_user) {
$to[] = $role_user->mail->value;
}
}
$adhoc_emails = array_map('trim', explode(',', $notification->emails));
foreach ($adhoc_emails as $email) {
$to[] = $email;
}
$to = array_filter($to);
$to = array_unique($to);
$result = $mailManager
->mail('content_moderation_notifications', 'content_moderation_notification', implode(',', $to), $langcode, $params, NULL, true);
if ($result['result'] !== true) {
drupal_set_message(t('There was a problem sending the notification email.'), 'error');
}
}
}
}
}
}
function content_moderation_notifications_mail($key, &$message, $params) {
$options = array(
'langcode' => $message['langcode'],
);
switch ($key) {
case 'content_moderation_notification':
$message['from'] = \Drupal::config('system.site')
->get('mail');
$message['subject'] = t('@subject', array(
'@subject' => $params['subject'],
), $options);
$message['body'][] = $params['message'];
break;
}
}