Adding a Custom Status Message to a Screen
There are numerous instances in which a plugin needs to return a user to a pre-existing screen in the app and display some kind of status message to the user indicating that yes, indeed, what they just clicked on actually did something and it worked (or failed, depending).
Adding a status message requires a developer to use a transformer callback to inject some Movable Type template code into a page. Transformer plugins are covered in great detail in the “Altering the UI” section of this guide, but for now we will show a quick example for this specific use case, as this will come in handy whenever working with list actions and/or page actions.
First, you need to register your transformer callback in your config.yaml
like so:
name: Example Plugin for Movable Type
id: Example
description: This plugin is an example plugin for Movable Type.
version: 1.0
callbacks:
MT::App::CMS::template_source.edit_entry: $Example::Example::Plugin::xfrm_edit
Then, in your Plugin.pm
file you will need to add the following subroutine:
sub xfrm_edit {
my ($cb, $app, $tmpl) = @_;
my $slug = <<END_TMPL;
<mt:if name="object_saved">
<mtapp:statusmsg
id="super-page"
class="alert">
<__trans phrase="The object has been saved.">
</mtapp:statusmsg>
</mt:if>
END_TMPL
$$tmpl =~ s{(<div id="msg-block">)}{$1$slug}msg;
}
Finally, we need a way to trigger the <mt:if name="object_saved">
to evaluate to true. To do this we rely on the fact that the value of any query string parameter is accessible via the corresponding template. Therefore, if we can add “object_saved=1” to the query string of the Edit Entry screen, then the status message we have inserted will appear. To do that there are a number of techniques we can use in any handler you implement. They are:
$app->add_return_arg()
$app->add_return_arg( object_saved => 1 );
return $app->call_return;
$app->redirect()
return $app->redirect('?__mode=foo&object_saved=1');
$app->param()
$app->param('object_saved',1);
return $app->mode('foo');