Drupal 6 Module: Status Messages Alter

9 Jul 2010 Ferado

This module is sadly, no more.

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 :)


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?
// you can even search specific types of messages
$matches = $messages->contains(‘happy’, ‘status’);
// or
$matches = $messages->contains(‘funky’, ‘error’);

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
// now, let’s add something
$messages->add(t(‘No regex here!’));

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.


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:



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

7 comments on “Drupal 6 Module: Status Messages Alter

  1. Abhijit

    One of my colleagues was having a tough time with system/module status messages as the client was wanting a different set of messages. So he had to edit some of the modules. This gave me the idea to write a module that will override the messages; but before proceeding with anything I did a search on drupal.org and found your module! Thanks for the module! By the way, I was thinking of a module that will show a list of all modules as an admin interface and clicking any module will show the list of messages it prints. User will be able to add a custom message that will override a particular message. I hope you will consider something in that line in future.

  2. Daniel

    Hi, is it possible to use the contain-function (inside the MessagesAlter object) with the original english statustext, so i have no problems with different languages?

  3. sharvan

    hi friend,

    i am trying to use this module with ajax_register module.
    with out ajax_register module , its working but when we login by popup windo and input invalid data, than message_alter is not working.

    please help….