Archive for the ‘Joomla’ Category

Adding custom Joomla! toolbar buttons – Part 2

Friday, August 29th, 2008

Last 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);

Adding custom Joomla! toolbar buttons

Monday, August 25th, 2008

Whilst creating a new Joomla! component, not yet released, I found the need to add a custom button to the toolbar. A quick search through the books suggested the use of JToolBarHelper::custom(), but it didn’t go into details about where the icon should be located. On further searching in Google I found the following site

Temporaneità

This helped, but it wasn’t exactly what I was looking for, I don’t want someone to change the admin theme and all of a suddon loose the toolbar buttons…

So I looked deeper, remembering that you can add additional styles to the document, I decided to add a custom “toolbar” css file to my component and add it to the document every time that toolbar was used. The final changes to the files are shown below.

In the file toolbar.component.html.php added this inside of my _DEFAULT method

$document =& JFactory::getDocument();
$document->addStyleSheet('/administrator/components/com_component/css/admin.toolbar.css');
//JToolBarHelper::custom('task','normal image', 'rollover image', 'Button label');
JToolBarHelper::custom('sale','sale', 'sale', 'Sale');

I then created my new icon and added it the folder
/administrator/components/com_component/images/toolbar/. Following the format of the other toolbar icons, icon-32-{custom part}.png I named it icon-32-sale.png.

Create a new file /administrator/components/com_component/css/admin.toolbar.css

.icon-32-sale {
background-image:url(../images/toolbar/icon-32-sale.png);
}

Now I have a component with custom buttons that will work as expected regardless of the template the user has chosen.