Actions

J2.5

Using the JToolBar class in the frontend

From Joomla! Documentation

Copyedit.png
This Page Needs Your Help

This page is tagged because it NEEDS REVIEW. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.

Contents

Using JToolBar

It's now simpler to use JToolBar in the front end than it was in Joomla! 1.5 as it only requires two steps.

Step 1

Using the MVC architecture, add a function definition to view.html.php file in the view you wish to add the toolbar to.

        function getToolbar() {
                // add required stylesheets from admin template
                $document    = & JFactory::getDocument();
                $document->addStyleSheet('administrator/templates/system/css/system.css');
                //now we add the necessary stylesheets from the administrator template
                //in this case i make reference to the bluestork default administrator template in joomla 1.6
                $document->addCustomTag(
                        '<link href="administrator/templates/bluestork/css/template.css" rel="stylesheet" type="text/css" />'."\n\n".
                        '<!--[if IE 7]>'."\n".
                        '<link href="administrator/templates/bluestork/css/ie7.css" rel="stylesheet" type="text/css" />'."\n".
                        '<![endif]-->'."\n".
                        '<!--[if gte IE 8]>'."\n\n".
                        '<link href="administrator/templates/bluestork/css/ie8.css" rel="stylesheet" type="text/css" />'."\n".
                        '<![endif]-->'."\n".
                        '<link rel="stylesheet" href="administrator/templates/bluestork/css/rounded.css" type="text/css" />'."\n"
                        );
                //load the JToolBar library and create a toolbar
                jimport('joomla.html.toolbar');
                $bar =& new JToolBar( 'toolbar' );
                //and make whatever calls you require
                $bar->appendButton( 'Standard', 'save', 'Save', 'savetask', false );
                $bar->appendButton( 'Separator' );
                $bar->appendButton( 'Standard', 'cancel', 'Cancel', 'canceltask', false );
                //generate the html and return
                return $bar->render();
        }

Step 2

Now in the corresponding tmpl file, you simply need to add code like the following (added here to a default.php file in the tmpl folder of the view to which the code above was added to the view.html.php file. Note I say "like" the following, changes may be required. I'm including the <form> declaration html (the toolbar MUST be in a form with the id of "adminForm" in order for it to work.

<form action="index.php" method="post" id="adminForm" name="adminForm">
        <?php echo $this->getToolbar(); ?>
        <input type = "hidden" name = "task" value = "" />
        <input type = "hidden" name = "option" value = "com_yourcom" />
</form>
<div class="clr"></div>

You'll note the [div class="clr"] which you may or may not need depending on your own design requirements. If you don't add the div tag, slapping this code into an existing component front end will place the buttons to the right and level with the content on the left. Also, and as explained above, you'll need to add functions within the controller that is called when one of the toolbar buttons is pressed, to process the request gracefully.

Using JToolBar with Search Engine Friendly enabled (SEF)

When using the toolbar on the frontend,the standard buttons will sometimes bug out. This is usually because Search Engine Friendly urls is defined.

What happens is that instead of going to index.php?option=com_yourcom&view=&...#, it will go to index.php/friendlyurl# . This bugs out many users.

Using these two together can be done,however. There's two tricks to it. First, make a class called JButtonFrontend and extend JButton. For this, jimport('joomla.html.toolbar') has to be done,because this includes the standard button classes.

Just copy over the content of joomla.html.toolbar.button standard.php; then, change the fetchbutton function

//Goes inside JButtonFrontend class definition.
        public function fetchButton($type = 'Standard', $name = '', $text = '', $task = '', $list = true)
        {
                $i18n_text = JText::_($text);
                $class = $this->fetchIconClass($name);
                $doTask = $this->_getCommand($text, $task, $list);
 
                $html = "<a href=\"javascript: void( $doTask);\" onclick=\"$doTask\" class=\"toolbar\">\n";
                $html .= "<span class=\"$class\">\n";
                $html .= "</span>\n";
                $html .= "$i18n_text\n";
                $html .= "</a>\n";
 
                return $html;
        }

Then, in your mainfile (site/yourcom.php), use this:

JLoader::register('JButtonFrontend', dirname(__FILE__) . DS . 'buttons' . DS . 'frontend.php');

As you can see, I made a directory called 'buttons' in my /site directory. If you do so, don't forget to add it to your package xml file! (yourcom.xml).

You can now use the 'Frontend' button:

$this->bar->appendButton( 'Frontend', 'name', 'label', 'subcontroller.method', false );