You are here

message_notify_example.module in Message Notify 7.2

Same filename and directory in other branches
  1. 7 message_notify_example/message_notify_example.module

File

message_notify_example/message_notify_example.module
View source
<?php

/**
 * @file
 * Code for the Message notify example feature.
 */
include_once 'message_notify_example.features.inc';

/**
 * Implements hook_comment_insert().
 *
 * Send a message to the node author when a new comment is created.
 */
function message_notify_example_comment_insert($comment) {
  $node = node_load($comment->nid);
  if ($comment->uid == $node->uid) {

    // The comment author is also the node author, and we would ususally
    // return early here, but since this is an example, we send an email
    // anyway.
  }

  // Create a new message, assigned to the node author, and add a
  // reference to the comment, so we can later use tokens related to that
  // comment.
  $message = message_create('comment_insert', array(
    'uid' => $node->uid,
  ));
  $wrapper = entity_metadata_wrapper('message', $message);
  $wrapper->field_comment_ref
    ->set($comment);

  // Let message-notify deliver the email, and send the message for us.
  // We pass in the options the field names, that will be used to capture
  // the rendered message, and provide an email log.
  $options = array(
    'rendered fields' => array(
      'message_notify_email_subject' => 'field_message_rendered_subject',
      'message_notify_email_body' => 'field_message_rendered_body',
    ),
  );
  message_notify_send_message($message, $options);
  if (module_exists('message_subscribe')) {

    // If Message-subscribe exists, let this example module use it.
    message_subscribe_send_message('comment', $comment, $message, array(
      'email' => $options,
    ));
  }
}

Functions