Creating custom message templates with the messaging module and hook_messaging()

By shane
Tue, 2011-03-15 08:26
0 comments

Share with Others

As mentioned in a previous post, here is a quick shortened and simplified example that is similar to the code I used on a past Drupal site to create a message template. This example uses the messaging modules hook_messaging, it also declares a few token values that are passed through to the template.

/**
 * Implementation of hook_messaging.
 */
function MYMODULE_messaging($op, $arg0 = NULL, $arg1 = NULL) {
  switch ($op) {
    case 'message groups':
      $info['MYMODULE-notification'] = array(
        'module' => 'MYMODULE',
        'name' => t('MYMODULE Notifications'),
        'description' => t('Template for MYMODULE notifications.'),
      );
      return $info;
      break;
    case 'message keys':
      if ($arg0 == 'MYMODULE-notification') {
        return array(
          'subject' => t('Subject'),
          'header' => t('Header'),
          'main' => t('Content'),
          'footer' => t('Footer'),
        );
      }
      break;
    case 'messages':
      if ($arg0 == 'MYMODULE-notification') {
        return array(
          'subject' => t('Notification for [website_url]'),
          'header' => t("Greetings [user],"),
          'main' => array(
            t(''),
            t("The website [website_url] has returned an error."),
            t('You can log into your account at [site-url] for more details.'),
          ),
          'footer' => array(
              t(''),
              t('--'),
              t('This is an automatic message, thank you for using [site-name]'),
          ),
        );
      }
      break;
    case 'tokens':
      $tokens = array();
      // These are the token groups that will be used for this module's messages
      if ($arg0 == 'MYMODULE-notification') {
        $tokens = array('global', 'user', 'node', 'MYMODULE');
      }
      return $tokens;
  }
}
 
/**
 * Implements hook_token_values()
 */
function MYMODULE_token_values($type, $object = NULL, $options = array()) {
  switch ($type) {
    case 'MYMODULE':
      $values = array();
      if ($myobject = $object) {
        $values['website_url'] = $myobject->website_url;
      }
      return $values;
  }
}
 
/**
 * Implements hook_token_list(). Documents the individual
 * tokens handled by the module.
 */
function MYMODULE_token_list($type = 'all') {
  $tokens = array();
  if ($type == 'MYMODULE' || $type == 'all') {
    $tokens['MYMODULE']['website_url'] = t('The url of the website');
  }
  return $tokens;
}
 
/**
 * Sends website notification
 *
 * @param $nid
 *   The node id
 * @param $uid
 *   The user id
 * @param $website
 *   The website url the notification is for
 */
function MYMODULE_send_notification($nid, $uid, $website) {
  $user = user_load($uid);
  $node = node_load($nid);
 
  //build token object for replacements
  $my_object = new stdClass();
  $my_object->website_url = $website;
 
  //get the users preferred sending method
  $sending_method = messaging_method_default($user);
 
  $message['subject'] = messaging_message_part('MYMODULE-notification', 'subject', $sending_method );
  $message['body']['header'] = messaging_message_part('MYMODULE-notification', 'header', $sending_method);
  $message['body']['email'] = messaging_message_part('MYMODULE-notification', 'main', $sending_method);
  $message['body']['footer'] = messaging_message_part('MYMODULE-notification', 'footer', $sending_method);
 
  // Token replacement
  $objects['user'] = $user;
  $objects['node'] = $node;
  $objects['MYMODULE'] = $my_object;
  $objects['global'] = NULL; // This makes global tokens available too.
 
  $message = token_replace_multiple($message, $objects);
  $message['body'] = token_replace_multiple($message['body'], $objects);
 
  messaging_message_send_user($user, $message);
}

Now that is a good chunk of code just to get a messaging template, however it does many beneficial things that make it worth while. Now you will be able to go into the admin side to the messaging templates and override or modify it if you need to. The messaging module allows you to specify a template for different types of notifications. In my case I left the defaults for email but for SMS notifications I created a shortened version of the message.

Now to send a message I just need to call the send function in code. Something like this will suffice:

//the below code assumes you have a populated node object
//so I will just hard code the variables for a simple example
//you would want to dynamically grab this node but I will use nid of 12
$node = node_load(12);
$website = "<a href="http://codekarate.com";
MYMODULE_send_notification">http://codekarate.com";
MYMODULE_send_notification</a>($node->nid, $node->uid, $website);

Post new comment