You are here

function message_notify_example_comment_insert in Message Notify 8

Same name and namespace in other branches
  1. 7.2 message_notify_example/message_notify_example.module \message_notify_example_comment_insert()
  2. 7 message_notify_example/message_notify_example.module \message_notify_example_comment_insert()

Implements hook_comment_insert().

Send a message to the node author when a new comment is created.

File

modules/message_notify_example/message_notify_example.module, line 16
Message notify example module.

Code

function message_notify_example_comment_insert(Comment $comment) {

  // Get node where comment was posted.
  $node = $comment
    ->get('entity_id')
    ->first()
    ->get('entity')
    ->getTarget()
    ->getValue();

  // Call message notify service.
  $notifier = \Drupal::service('message_notify.sender');

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

  // Send message to message creator(in this case = node author).
  $notifier
    ->send($message);
}