By Sebastiaan de Jonge, published on Friday, April 16, 2010 at 07:00

Ever wondered how to create nice warnings, errors and notices in your own TYPO3 back-end modules and extensions? The way TYPO3 itself does this?

I'm working currently on a project, where I wanted to display a nice notice above the form when editing a content record. Now I could have done some hooking and marker overwriting.. but why not just use something that already exists? 🙂 If you have used 4.3.x already you've probably seen something similar around.

Creating our own messages

First of all these messages will only work in the back-end. So we need either a back-end module.. or we could of course use a hook in some other code that is run in the back-end. Next to that? Not really anything. We can create a nice message like this with just 7 lines of PHP code.

$flashMessage = t3lib_div::makeInstance(
    't3lib_FlashMessage',
    'Reward the user for actually doing something right today! <br /><i>&quot;Good boy!&quot;</i>',
    'Everything is O.K!',
    t3lib_FlashMessage::OK
);
t3lib_FlashMessageQueue::addMessage($flashMessage);

As you see, we are simply creating a new instance of t3lib_FlashMessage with some parameters and after that adding it to a message queue. We are passing on the following parameters to makeInstance() (in this order):

  • Of course the class name 't3lib_FlashMessage'
  • The text we would want to display in our message
  • The header of our message, we can leave this or the text field empty to just display one.
  • And finally the message type


These are all pretty straightforward, but there are more message types you may enter. Here's a list of possibilities you may choose:

t3lib_FlashMessage::NOTICE
t3lib_FlashMessage::INFO
t3lib_FlashMessage::OK
t3lib_FlashMessage::WARNING
t3lib_FlashMessage::ERROR

So how does it work?

Actually really simple! As you may have seen in the code, we just create an instance of t3lib_FlashMessage and add it to a queue. If we look more at the actually core functions, we will see 2 classes inside t3lib:

  • t3lib_FlashMessage (class.t3lib_flashmessage.php)
  • t3lib_FlashMessageQueue (class.tx_flashmessagequeue.php)


The first class is simply used to create new messages and push them onto the queue. The second class is responsible for the rendering and output of the messages. And that's it! 🙂

A demo

If you are interesting in all these outputs, I can suggest downloading the dummy extension found at the bottom of this article. It creates a back-end module inside the web module, where it displays all different message types.

Please note that this feature has been implemented since 4.3.0, the extension and code will not work in any lower TYPO3 versions.

UPDATE: Using Flash Messages in TYPO3 4.5 LTS

Since 4.5 I got some questions asking if something changed for the messaging system. So I took a look, and found out it actually did. Version 1.0.0 will not provide any output on TYPO3 4.5. And here's why.

The structure for messages has become more flexible, this also means that the render function for the messages is not called automatically anymore. Instead to display your flash messages you may use the following after adding them.

$allMessages = t3lib_FlashMessageQueue::renderFlashMessages();
echo $allMessages;

This means you can basically put the messages in any spot. I've added an updated version of the extension just for TYPO3 4.5 and up.

Happy coding!

Comments

Pim Broens
Pim Broens - Friday, April 16, 2010 at 07:50

that will come in handy multiple times.

BenjaminS
BenjaminS - Friday, April 16, 2010 at 10:43

This is, in my opinion, also a "Pimp my Backend" feature 🙂
Just installed and checked the code, easy to use!

Aleksandar MIlovac
Aleksandar MIlovac - Saturday, January 29, 2011 at 11:57

Hi Sebastiaan,

I've played with this extension on my 4.4.6 installation, but after upgrade to LTS, I cannot see those messages anymore. Did something change in T3 API in meanwhile?

Best regards,Aleksandar

Sebastiaan de Jonge
Sebastiaan de Jonge - Wednesday, February 9, 2011 at 09:28

Hi Aleksandar,

I haven't given this extension a try in 4.5 yet, but it's possible something changed to the way these messages are rendered. I guess I will take a look today, since I'm a bit curious. I have heard other reports of people having problems with flash messages and 4.5 though, so I guess something changed.

I'll let you know later today 🙂

Cheers,Sebastiaan

Benjamin Serfhos
Benjamin Serfhos - Monday, May 23, 2011 at 12:36

To add the flashmessages like before, on 4.4.x. Just add an extra true parameter. You will have somehting like this:

$flashMessage = t3lib_div::makeInstance(
    't3lib_FlashMessage',
    'Reward the user for actually doing something right today! &quot;Good boy!&quot;',
    'Everything is O.K!',
    t3lib_FlashMessage::OK,
    1
);

This add the message to the session storage, and will be loaded in the current session event.

Fedir
Fedir - Friday, July 18, 2014 at 08:33

Nice post, thanks.