0

I'm developing a Drupal based forum and I'm using the Message stack modules combined with a tweaked version of this custom module. https://github.com/Lullabot/message_integration 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)) {
    return;
  }

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

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

  // 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.

Your Answer

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

Browse other questions tagged or ask your own question.