1

We are using the following modules below for content moderation. Currently have we have 5 people with the Content Approver role and all of them are sent an email when a state transition occurs. There is a need to send email alerts of moderation transitions for certain content types that need approval to just 2 out of the 5 Approvers.

How to send an email to select set of users when changing moderation state?

Expected Results:

When a content editor marks a page ready for Review, they are allowed to manually select one or more users to send an email alert to regardless of Role.

Enabled Modules:

  1. Workbench v8.x-1.0
  2. Workbench Email v8.x-1.0-alpha7
0
+50

As per @Matoeil's answer you could probably do this with rules.

You could add an entity reference field, limited to 5 values (or more if you want), and you could use a view to limit the available options (users) based on role. Then you could use rules send an email to the referenced users.

  • having the entity reference field did the trick. – usernameabc Jun 13 '18 at 18:17
1

you can maybe use rules module

alternatively create a custom module and you can inspire from this, that is used with core content moderation module:

use  \Drupal\user\Entity\User;

/**
 *
 *
 * Envoie de mail en fonction des transitions le modération de contenu.
 *
 * @param \Drupal\Core\Entity\EntityInterface $entity
 */
function my_module_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity->getEntityTypeId() !== 'content_moderation_state') {
    return;
  }

  $article_id = $entity->get('content_entity_id')->getString();
  $article = \Drupal\node\Entity\Node::load($article_id);
  if ($article->bundle() !== 'article') {
    return;
  }

  global $base_url;

  $article_title = $article->get('title')->getString();
  $article_url = $base_url . "/node/" . $article->get('nid')->getString();

  $author_name = $article->getOwner()->get('field_cn_ldap')->getString();
  $author_email = $article->getOwner()->getEmail();
  $current_user_uid = \Drupal::currentUser()->id();
  $current_user_name = \Drupal\user\Entity\User::load($current_user_uid)
    ->get('field_cn_ldap')
    ->getString();
  $is_author = ($current_user_name == $author_name);    

  $uids = \Drupal::entityQuery('user')
    ->condition('status', 1)
    ->condition('roles', 'redacteur_en_chef')
    ->execute();
  ;
  $users = User::loadMultiple($uids);

  $emails_redacteur_en_chef = [];
  foreach ($users as $user) {
    $emails_redacteur_en_chef[] = $user->getEmail();
  }
  $emails_redacteur_en_chef = implode(',', $emails_redacteur_en_chef);

  $moderation_state = $entity->get('moderation_state')->getString();
  $moderation_state_original = '';
  if ($entity->original) {
    $moderation_state_original = $entity->original->get('moderation_state')
      ->getString();
  }
  $is_transition_change = ($moderation_state !== $moderation_state_original);

  include 'includes/notification/messages.php';
  include 'includes/notification/titles.php';
  $send_to = $author_email;
  $params['node_title'] = $title_mod;
  $params['message'] = $message_same_state;
  $send = FALSE;

  switch ($moderation_state) {
    case 'draft':
      if (!$is_transition_change && !$is_author) {
        $send = TRUE;
      }
      break;

    case 'relecture':
      if (!$is_transition_change && !$is_author) {
        $send = TRUE;
      }
      elseif ($is_transition_change) {        
        $send_to = "joe@google.fr";
        $params['node_title'] = $title_prop;
        $params['message'] = $message_prop;
        $send = TRUE;
      }
      break;

    case 'relecture1':
      if (!$is_transition_change && !$is_author) {
        $send = TRUE;
      }
      elseif ($is_transition_change) {     
        $send_to = "whatever@yahoo.com";
        $params['node_title'] = $title_relec1;
        $params['message'] = $message_relec1;
        $send = TRUE;
      }
      break;

    case 'relecture2':
      $send_to = $emails_redacteur_en_chef;
      $params['node_title'] = $title_relec2;
      $params['message'] = $message_relec2;
      $send = TRUE;
      break;

    case 'published':
      if (!$is_transition_change) {
        $send_to .= ',';
        $send_to .= $emails_redacteur_en_chef;
        $send = TRUE;
      }
      else {

        $send_to = "all@hotmail.com";
        $params['node_title'] = $title_pub;
        $params['message'] = $message_pub;
        $send = TRUE;
      }
      break;

    case 'maj':     
      $send_to = "whatever@wahtever.com";
      $params['node_title'] = $title_maj;
      $params['message'] = $message_maj;
      $send = TRUE;
      break;

    default:
      break;
  }

  if ($send) {
    $mailManager = \Drupal::service('plugin.manager.mail');
    $module = "amu_dud_rules";
    $key = 'state_changed_action';
    $to = $send_to;

    $langcode = \Drupal::currentUser()->getPreferredLangcode();

    $result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);

    if ($result['result'] !== TRUE) {
      drupal_set_message(t('There was a problem sending notifications.'), 'error');
    }
    else {
      drupal_set_message(t("A notification has been sent to $to"));
    }
  }
}

/**
 * Implements hook_mail().
 */
function my_module_mail($key, &$message, $params) {
  $options = [
    'langcode' => $message['langcode'],
  ];
  switch ($key) {
    case 'state_changed_action':
      //$message['from'] = \Drupal::config('system.site')->get('mail');
      $from = 'from@from.com';

      $message['from'] = $from;
      $message['headers']['From'] = $from;
      $message['headers']['Sender'] = $from;
      $message['headers']['Return-Path'] = $from;
      $message['subject'] = t('@title', ['@title' => $params['node_title']], $options);
      $message['body'][] = $params['message'];
      break;
  }
}
  • if i understand this correctly then the moderation state is 'relecture1' then only whatever@yahoo.com is sent the email, but if moderation state is relecture2 then it sends it to everyone where role = redacteur_en_chef? – usernameabc May 31 '18 at 16:14
  • yes , depending on state and transition u can define send_to and message content. note that this works for content moderation module , for workflow module you'll have to change some code, starting with the first line if ($entity->getEntityTypeId() !== 'content_moderation_state') – Matoeil Jun 1 '18 at 8:21
  • if you look at the expected results, we are looking for a way while marking a page ready for review to allow for a content editor to manually select what users to send the email to. Looking at the code provided it is hardcoded, but there will be scenarios where we may not want to always send to whatever@yahoo.com, we may to send to someone else due to some reason. We tried and this did not provide that. – usernameabc Jun 1 '18 at 16:31
  • 1
    We tried and this did not provide that. This website is not a code writing service. In addition, to quote the answer, "you can inspire from this". – No Sssweat Jun 13 '18 at 5:59
  • @NoSssweat I don't expect any question to result in code. The reason for my comment is based on the excepted results they are allowed to manually select one or more users to send an email alert to regardless of Role. – usernameabc Jun 13 '18 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.