Drupal7: Advanced Message Module Usage

message_ adminPreviously in the Building a Simple Email Log in Drupal7 blog entry I mentioned the Message module when creating a Sent Email Log for a client web application. The message module allows you to create activity streams of events in a system or store a history ledger of events. Messages support Drupal7 tokens out of the box — but they can also be more advanced; recently I was asked how to create a Message that was shown in the following requirements. Can Drupal do this?

Show a Message with the following text for Users:
"X has posted a new thread in the Y forum."
I've got a view that spits all of these out.
I now want to create a new view that shows the same messages, but in this view, I want to change the output of the above message to:
"You have posted a new thread in the Y forum."
Every time the node's author matches the currently authored user.

Sure Drupal can do this! The Message module supports some advanced tokenization methods, from its README file:

* Custom message arguments (Custom callbacks)
  When creating a message, it's possible to store custom arguments that will be replaced when presenting the message.
  E.g. If the message was created with an argument called "@sometext", it will get inserted to the message text (On display time) whenever the string "@sometext" is encountered.
  This method also supports custom call-back functions with optional arguments stored on the message; In order to use a callback, create the message with an argument such as:
  '!replaced-by-foo' => array(
    'callback' => 'foo',
    'callback arguments' => array('x', 'z')
  That will get the string '!replaced-by-foo' in the message body to be replaced by the output of calling foo('x', 'z').

So we can use a custom callback in the message to change the value of a part of the Message upon display. We could define a message with text like, “!user has posted a new thread in forum Y.“.

When we create an instance of the message we can pass $arguments with a callback function to show our message. This callback is run every time the message is displayed to the screen — thus making a dynamic message! This code could look like:

$arguments['!user'] = array(
  'callback' => '_user_context_label',
  'callback arguments' => NULL,
  'pass message' => FALSE,
$message = message_create(
  array('arguments' => $arguments)

This message would then call our custom defined function _user_context_label to provide a string fragment for our message. This function could look like:

function _user_context_label() {
  global $user; // The logged in user.
  $thread_user = current_thread->get_user();
  return ($user->uid == $thread_user->uid) ? t('You') : $thread_user->username;

I hope this helps to show you how to make more dynamic messages using the Message module. Happy Coding!

Looking for quality Drupal Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *