Adding custom Joomla! toolbar buttons - Part 2
Friday, August 29th, 2008Last time was a custom image, this time I wanted to add a button to allow the user to navigate to another view of the component. Joomla! toolbar buttons expect that the buttons on the toolbar are all related to the set of data in the “adminForm” showed below it, in my case I just wanted to allow the user to navigate to a “dashboard” style view of the component. A bit of digging through the Joomla! libraries and I realised that no such thing existed via the JToolbarHelper class.
Digging a little deeper I noticed a “back” button existed - not quite what I wanted but it gave me the code I needed.
/**
* Writes a cancel button that will go back to the previous page without doing
* any other operation
* @since 1.0
*/
function back($alt = 'Back', $href = 'javascript:history.back();')
{
$bar = & JToolBar::getInstance('toolbar');
// Add a back button
$bar->appendButton( 'Link', 'back', $alt, $href );
}
I then set about adding the following new method to my TOOLBAR_component class
function buttonLink($name, $alt, $href)
{
$bar = & JToolBar::getInstance('toolbar');
// Add a button
$bar->appendButton( 'Link', $name, $alt, $href );
}
In the above code, $name is used to construct the class name of the toolbar button, $alt as you would expect is the alt attribute of the button, and $href the link to the page.
Now in my toolbar methods in the TOOLBAR_component class I just need to use self::buttonLink() to add a new button with a normal <a href="">
self::buttonLink('dashboard', 'Dashboard', 'index.php?option='.$option);

