Actions

J3.x

Difference between revisions of "Using the JToolBar class in the frontend"

From Joomla! Documentation

(new page for 3.1, need version checking)
 
 
(9 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{page|needs review}}{{RightTOC}}
 
{{page|needs review}}{{RightTOC}}
 +
 +
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 ==
 
== 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.
 
It's now simpler to use JToolBar in the front end than it was in Joomla! 1.5 as it only requires two steps.
 +
 
{{clear}}
 
{{clear}}
 
===Step 1===
 
===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.
+
Add the toolbar to your view display function
 
<source lang="php>
 
<source lang="php>
function getToolbar() {
+
function display($tpl = null)
// add required stylesheets from admin template
+
{
$document    = & JFactory::getDocument();
+
...
$document->addStyleSheet('administrator/templates/system/css/system.css');
+
// Set the toolbar
//now we add the necessary stylesheets from the administrator template
+
$input->set('hidemainmenu', true);
//in this case i make reference to the bluestork default administrator template in joomla 1.6
+
$this->addToolbar();
$document->addCustomTag(
+
parent::display($tpl);
'<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();
+
}
+
 
</source>
 
</source>
  
 
===Step 2===
 
===Step 2===
 
+
Add an addToolbar() function to view.html.php file in the view you wish to add the toolbar to (this function could possibly be placed in a helper function).
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.
+
<source lang="php>
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.
+
protected function addToolbar()
<source lang="php">
+
<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>
+
</source>
+
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
+
<source lang="php">
+
//Goes inside JButtonFrontend class definition.
+
public function fetchButton($type = 'Standard', $name = '', $text = '', $task = '', $list = true)
+
 
{
 
{
$i18n_text = JText::_($text);
+
$input = JFactory::getApplication()->input;
$class = $this->fetchIconClass($name);
+
               
$doTask = $this->_getCommand($text, $task, $list);
+
// Add whatever buttons you require
 
+
JToolBarHelper::save('yourcom.save');
$html = "<a href=\"javascript: void( $doTask);\" onclick=\"$doTask\" class=\"toolbar\">\n";
+
JToolbarHelper::divider();
$html .= "<span class=\"$class\">\n";
+
JToolBarHelper::cancel('yourcom.cancel');
$html .= "</span>\n";
+
JToolbarHelper::divider();
$html .= "$i18n_text\n";
+
$html .= "</a>\n";
+
 
+
return $html;
+
 
}
 
}
</source>
 
 
Then, in your mainfile (site/yourcom.php), use this:
 
<source lang="php">
 
JLoader::register('JButtonFrontend', dirname(__FILE__) . DS . 'buttons' . DS . 'frontend.php');
 
</source>
 
 
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:
 
<source lang="php">
 
$this->bar->appendButton( 'Frontend', 'name', 'label', 'subcontroller.method', false );
 
 
</source>
 
</source>
  
 
[[Category:Development]]
 
[[Category:Development]]

Latest revision as of 14:22, 26 June 2015

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.

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

Add the toolbar to your view display function

function display($tpl = null)
{
	...
	// Set the toolbar
	$input->set('hidemainmenu', true);
	$this->addToolbar();
	parent::display($tpl);
}

Step 2

Add an addToolbar() function to view.html.php file in the view you wish to add the toolbar to (this function could possibly be placed in a helper function).

	protected function addToolbar()
	{
		$input = JFactory::getApplication()->input;
 
		// Add whatever buttons you require
		JToolBarHelper::save('yourcom.save');
		JToolbarHelper::divider();
		JToolBarHelper::cancel('yourcom.cancel');
		JToolbarHelper::divider();
	}