Actions

J3.2

Using the JToolBar class in the frontend

From Joomla! Documentation

Revision as of 15:56, 10 December 2013 by Erikj (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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

NOTE: This documentation was not previously updated for Joomla! 3.2. I have made some updates to bring it closer to what is needed for Joomla! 3 but admittedly, I still don't have this functionality working. Additional corrections and contributions would be greatly appreciated! Erik Jorgensen 12/10/2013.

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/isis/css/template.css');
                //now we add the necessary stylesheets from the administrator template
                //in this case i make reference to the isis default administrator template in joomla 3.2
                $document->addCustomTag( '<link href="administrator/templates/isis/css/template.css" rel="stylesheet" type="text/css" />'."\n\n" );
                //load the JToolBar library and create a toolbar
                jimport('cms.html.toolbar');
                $bar =& new JToolBar( 'toolbar' );
                //and make whatever calls you require
                $bar->appendButton( 'Standard', 'save', 'Save', 'yourcom.save', false );
                $bar->appendButton( 'Separator' );
                $bar->appendButton( 'Standard', 'cancel', 'Cancel', 'yourcom.cancel', 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, create a buttons folder under your main component folder and create a frontend.php file with a class called JToolbarButtonFrontend which extends JToolbarButton. Include a jimport('cms.html.toolbar') statement to include the standard button classes.

This file will need the following code to create the search engine friendly front end buttons.

defined('_JEXEC') or die('Restricted access');
 
// import Joomla view library
jimport('cms.html.toolbar');
 
/**
 * Search Engine Friendly Frontend Buttons
 */
class JToolbarButtonFrontend extends JToolbarButton {
 
        public function fetchButton($type = 'Frontend', $name = '', $text = '', $task = '', $list = true)
        {
                $i18n_text = JText::_($text);
                $class = $this->fetchIconClass($name);
                $doTask = $this->_getCommand($text, $task, $list);
 
                $html = "<button onclick=\"$doTask\" class=\"btn btn-small\">\n";
                $html .= "<span class=\"$class\">\n";
                $html .= "</span>\n";
                $html .= "$i18n_text\n";
                $html .= "</button>\n";
 
                return $html;
        }
 
        /**
     * Get the button CSS Id
     *
     * @param   string  $type  Not used.
     * @param   string  $html  Not used.
     * @param   string  $id    The id prefix for the button.
     *
     * @return  string  Button CSS Id
     *
     * @since   3.0
     */
    public function fetchId($type = 'Frontend', $html = '', $id = 'frontend')
    {
        return $this->_parent->getName() . '-' . $id;
    }
 
        /**
     * Get the JavaScript command for the button
     *
     * @param   string   $name  The task name as seen by the user
     * @param   string   $task  The task used by the application
     * @param   boolean  $list  True is requires a list confirmation.
     *
     * @return  string   JavaScript command string
     *
     * @since   3.0
     */
    protected function _getCommand($name, $task, $list)
    {
        JHtml::_('behavior.framework');
        $message = JText::_('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST');
        $message = addslashes($message);
 
        if ($list)
        {
            $cmd = "if (document.adminForm.boxchecked.value==0){alert('$message');}else{ Joomla.submitbutton('$task')}";
        }
        else
        {
            $cmd = "Joomla.submitbutton('$task')";
        }
 
        return $cmd;
 
    }
}

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

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

Be sure to add the new "buttons" directory to your package xml file! (yourcom.xml)

You can now use the 'Frontend' buttons in the getToolbar methods in your view.html.php files:

                //load the JToolBar library and create a toolbar
                jimport('cms.html.toolbar');
                $this->bar = new JToolBar( 'toolbar' );
                //and make whatever calls you require
                $this->bar->appendButton( 'Frontend', 'save', 'Save', 'yourcom.save', false );
                $this->bar->appendButton( 'Separator' );
                $this->bar->appendButton( 'Frontend', 'cancel', 'Cancel', 'yourcom.cancel', false );
                //generate the html and return
                return $this->bar->render();