Message Subscribe is ignoring flag and sending notifications to everyone

au flag

I'm developing a Drupal based forum and I'm using the Message stack modules combined with a tweaked version of this custom module. The idea is that all users get an email when a new thread is posted, but only users to subscribe to a thread get notification for comments left in that thread. The part where people get emails for new threads is working, but when a comment is left, all users get an email notification about that too. At this point I can't tell if the issue is with the custom module or with Message Subscribe. I've turned on debug but I don't see any messages about it deciding who to and not send the emails to, just that its preparing the message and sending it.

My custom module isn't that different from what Lullabot set up.

This is the code that creates the subscriber list:

function message_integration_subscribe_options_comment() {
  $query = \Drupal::entityQuery('user')
    ->condition('status', 1);
  $query->condition('uid', [0, 1], 'NOT IN');
  $subscribe_uids = $query->execute();

  $notifiers = ['email'];
  $subscribe_options = [
    'notify message owner' => FALSE,
    'uids' => [],
  foreach ($subscribe_uids as $uid) {
    $subscribe_options['uids'][$uid] = new DeliveryCandidate([], $notifiers, $uid);
  return $subscribe_options;

And here's the code for when a comment is left:

function message_integration_comment_insert(Comment $comment) {

  // See if this handling should be skipped.
  $config = \Drupal::config('message_integration.settings');
  $skip = $config->get('skip');
  if (!empty($skip)) {

  $node = $comment->get('entity_id')->first()->get('entity')->getTarget()->getValue();
  if (!in_array($node->bundle(), MESSAGE_INTEGRATION_CONTENT_TYPES)) {

  // Create a message.
  $message = Message::create([
    'template' => 'create_comment',
    'uid' => $node->getOwnerId(),
  $message->set('field_comment_reference', $comment);
  $message->set('field_published', $comment->isPublished());

  // Queue messages to notify all the node subscribers about new comment.
  $subscribers = \Drupal::service('message_subscribe.subscribers');
  // Create a custom subscriber list to notify all active users.
  // This is an end run around the normal flag subscription system since we
  // don't want to subscribe every user to every piece of content.
  $subscribe_options = message_integration_subscribe_options_comment();
  $options = $subscribe_options;
  foreach ($subscribe_options['uids'] as $uid => $values) {
    $options['uids'] = [$uid => $values];
    $subscribers->sendMessage($node, $message, [], $options);

I've been going in circles and pulling my hair out, so any help would be appreciated.

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.