9 Jul 2010 Ferado

Drupal 6 Module: Status Messages Alter

In January 2010 I released Status Messages Alter, my first Drupal module released to the community. I’ve created many modules over the years, I finally had the time to give something back to my fellow Drupal enthusiasts. I’m very excited about this.

What does this module do?

The module provides developers with a new hook called hook_message_alter. This hook comes handy when you have annoying messages being displayed and you want to get rid of them, or if you want to have your own custom code fire off when specific messages occur. This is an api module and does not offer any kind of theming. You’re allowed to override that yourself. I’ll explain that later.

How do I install the module?

Simply place the module in your modules directory and enable the module. If you’re upgrading from a previous version, you may see a warning message. This warning message is there to notify you that you may have to check your other enabled modules that implement hook_message_alter for compatibility. This isn’t really going to be an issue because only a few people downloaded that first release :)

Demo

How do I use the module?

Normally when I create a Drupal site I dedicate a specific module for implementation specific code. That way I can create other modules that are reusable. Drupal makes this a great approach. I’m not your boss, so if you want to put this hook in whatever module you want, go for it.

Now, in your module you’re going to have a hook that will look similar to the following:

[cc lang=”php”]
function yourmodule_message_alter(&$messages) {
//TODO: your code here
// Example code:
$matches = $messages->contains(‘michael is awesome’);
// TODO: do something with it?
$messages->remove($matches);
// you can even search specific types of messages
$matches = $messages->contains(‘happy’, ‘status’);
// or
$matches = $messages->contains(‘funky’, ‘error’);
}
[/cc]

The preceding code simply searches all of the status messages (warning, error, status, custom ones) for the phrase “michael is awesome.” Since I’m awesome and I wrote the code, I will see results :) After I get the results, I’m removing them. In the real world you would probably do something with the results. You’re a smart person, I’m sure you can come up with proper use cases.

Regular Expressions

Sometimes you need to do regular expression searches, here’s how to do it with the module:

[cc lang=”php”]
function yourmodule_message_alter(&$messages) {
// let’s do a simple regex search
$matches = $messages->match(‘/regex/’);
// let’s remove our matches
$messages->remove($matches);
// now, let’s add something
$messages->add(t(‘No regex here!’));
}
[/cc]

Upcoming Features

As you can tell, this module has a lot of potential. I’m thinking of adding the following things:

  • Regular expression search
  • Find first occurrence
  • Find last occurrence
  • Triggers & Actions capabilities
  • A simple UI

Feel free to chime in and add whatever features you’d like to see. You can use my comment form below or use your Drupal login and submit a feature request through the official Drupal issue tracker.

Conclusion

I’ve told you about my new module and I’ve shown you how to use it. Furthermore, I’ve told you about some of my plans with the module. The only thing remaining in my mind is to let the people that stumbled onto my blog and found this page from Google know where to download the module. The link is as follows:

http://drupal.org/project/messages_alter

or

[source label=”Visit Project Page” href=”http://drupal.org/project/messages_alter”]