blog.Resource

Archive:

News-Feeds:


RSS 2.0
RSS 0.91
RDF
ATOM 0.3
June 8, 2010

4.3 Treasure Trove: Flash messages

Category: Core

By: Fran├žois Suter

You've seen them all over the TYPO3 backend since version 4.3, but did you know they were very easy to use in your own extensions?

Flash messages in action in the Scheduler

Flash messages were introduced to provide a standard way of displaying status messages to the user. Before that every backend module would display success or error messages in its own way. The transition is not yet complete and some areas still don't use flash messages, but it's definitely the way we're going. Note that all this has nothing to do with Adobe Flash, it's just the way this type of message is called.

Flash messages can either be displayed directly or put into a queue. There's a class called t3lib_FlashMessageQueue which contains a static member variable to store messages. When a BE module is rendered, the template class calls on the queue to retrieve the messages and displays them at the top of the BE module or replaces the ###FLASHMESSAGES### marker, if found in the module's template. Here's a sample code for adding a message to the queue (taken from the Scheduler):

$message = t3lib_div::makeInstance(
    't3lib_FlashMessage',
    $message,
    '',
    $severity
);
t3lib_FlashMessageQueue::addMessage($message);

On top of this built-in mechanism, it's possible to use flash messages at your own will inside your BE modules. Just create an instance of a message and ask it to render itself. Here's what such code might look like:

$message = t3lib_div::makeInstance(
    't3lib_FlashMessage',
    'This is an error message',
    '',
    t3lib_FlashMessage::ERROR
);
$content .= $message->render();

Note that the t3lib_FlashMessage class defines a number of constants that represent the status level of the message. Available constants are NOTICE, INFO, OK, WARNING and ERROR.

It is perfectly possible to use the flash message class in the frontend too, but the styles for proper rendering will not be defined. They should be added to your web site's stylesheet.


comments

comment #1
Gravatar: Eric Eric June 9, 2010 00:15
Hey flash messages are great, thanks for taking the time to share this.

comment #2
Gravatar: Pim Broens Pim Broens June 11, 2010 10:14
Thanks Fran├žois, for the Treasure Trove posts. These will definitly come in handy one day.
Looking forward to the next.

comment #3
Gravatar: Carsten Carsten June 14, 2010 16:30
Very nice feature, THX!

comment #4
Gravatar: Gry Online Gry Online June 18, 2010 07:13
Another great solution, like always here, thanks

comment #5
Gravatar: Pogoda Pogoda June 27, 2010 15:59
it's very ingenious solution

comment #6
Gravatar: susanislaid susanislaid July 8, 2010 11:50
http://forum.lunohodov.net/index.php

Sorry, comments are closed for this post.