Skip to main content

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

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:

  1. /**
  2.  * Implements hook_message_alter().
  3.  */
  4. function MYMODULE_message_alter(&$message, $info) {
  5.  
  6.     $account = $message->account;
  7.     if (isset($message->notifications['events'])) {
  8.       foreach ($message->notifications['events'] as $event) {
  9.         if ($event->module == 'node' && $event->type == 'node') {
  10.           $node = $event->objects['node'];
  11.  
  12.           // Find our custom node type
  13.           switch ($node->type) {
  14.             case 'MY_CUSTOM_NODE':
  15.               // Setup our custom header.
  16.               $message->body['header'] = "MY CUSTOM HEADER";
  17.  
  18.               // Setup our custom body.
  19.               $message->body['event'] = "MY CUSTOM BODY";
  20.  
  21.               // Setup our custom footer.
  22.               $message->body['footer'] = "MY CUSTOM FOOTER";
  23.             break;
  24.           }
  25.         }
  26.       }    
  27.     }
  28. }


Comments