What is ButtonService
ButtonService is a service that tracks/creates all buttons for a page. Buttons are stored in session, so when you click from one page to the next, ButtonService can pull out important information about what action was taken, and any other important information that you need to continue (ie user id, title, etc).
Pre-Click
To create a button using button service is very easy. This code should be located inside your views generateParams function. The following code will build up a simple button:
Perl
use Solstice::ButtonService;
.
.
.
my $button_service = Solstice::ButtonService->new();
my $edit_button = $button_service->makeButton({
label => 'Edit',
title => 'Edit your form',
action => 'edit_form',
attributes => {
form_id => $form_id,
},
});The keys label and title are just for display purposes. The action key will transition you from your current state to the state you define in your pageflow xml document. So if we were in the state home_screen_view and wanted to get to the state edit_form_view, the pageflow.xml file would have to have something like this:
XML
<state name=
"home_screen_view"
>
<transitions>
<transition action=
"edit_form"
state=
"edit_form_view"
>
A further explanation of pageflows can be found Defining page flows in your application.
The next question to answer is, how do we get the button showing up on the screen? First we need to add it to our view, and then we need to tell the template where to paint the button. What if we want a text link or a pop-in? ButtonService's API takes care of all of that. Go check out the pod if you want to see all the different kinds of buttons you can build. So, to first add the button to the view:
Perl
$self->setParam( 'edit_button', $edit_button->getHTML());Now we must tell the template that it needs to paint our new button. The following template would do the trick.
Perl
Welcome to the home screen! Please press the edit button to edit your form.<p />
<!-- tmpl_var name='edit_button' -->Post-Click
After the user has clicked you will want to grab out information that was passed into your button. ButtonService provides this. You can grab out attributes by using the getAttribute method.
Perl
use Solstice::ButtonService;
.
.
.
my $button_service = Solstice::ButtonService->new();
my $form_id = $button_service->getAttribute('form_id');