Create different messages per content type with Drupal's Messaging/Notifications framework.

Profile picture for user Phil Frilling
By Phil Frilling, 17 January, 2014
A recent project required me to create a different notification message per content type. To do this you can use the hook_message_alter() function to override the default message. I borrowed heavily from the code in file_message_alter() The code I used is below:

/**
 * Implements hook_message_alter().
 */
function MYMODULE_message_alter(&$message, $info) {

    $account = $message->account;
    if (isset($message->notifications['events'])) {
      foreach ($message->notifications['events'] as $event) {
        if ($event->module == 'node' && $event->type == 'node') {
          $node = $event->objects['node'];
          
          // Find our custom node type
          switch ($node->type) {
            case 'MY_CUSTOM_NODE':
              // Setup our custom header.
              $message->body['header'] = "MY CUSTOM HEADER";

              // Setup our custom body.
              $message->body['event'] = "MY CUSTOM BODY";

              // Setup our custom footer.
              $message->body['footer'] = "MY CUSTOM FOOTER";
            break;
          }
        }
      }    
    }
}