Creating a toolbar for your component: Difference between revisions

From Joomla! Documentation

Wilsonge (talk | contribs)
Change 3.1 to 3.x and add version tag
m style
 
(11 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{version|2.5,3.x}}
<noinclude><languages /></noinclude>
Note: This applies when using the MVC structure.
<noinclude>{{Joomla version|version=3.x}}</noinclude>
<translate><!--T:1-->
Note: This applies when using the MVC structure.</translate>


By default the Joomla Toolbar uses a Delete, Edit, New, Publish, Unpublish and Parameters buttons. To use them in your component you must write the following code in the view.html.php of your view file (below your display function).
<translate><!--T:2-->
By default the Joomla Toolbar uses a Delete, Edit, New, Publish, Unpublish and Parameters buttons. To use them in your component you must write the following code in the view.html.php of your view file (below your display function).</translate>
<source lang="php">
<source lang="php">
       JToolBarHelper::title( 'your_component_view_page_title', 'generic.png' );
       JToolBarHelper::title(string $title, string $icon);
       JToolBarHelper::deleteList(); // Will call the task/function "remove" in your controller
       JToolBarHelper::deleteList(string  $msg, string  $task, string  $alt); // Will call the task/function $task in your controller
       JToolBarHelper::editListX(); // Will call the task/function "edit" in your controller
       JToolBarHelper::editList(string  $task, string  $alt); // Will call the task/function $task in your controller
       JToolBarHelper::addNewX(); // Will call the task/function "add" in your controller
       JToolBarHelper::addNew(string $task, string $alt, boolean  $check); // Will call the task/function $task in your controller
       JToolBarHelper::publishList(); // Will call the task/function "publish" in your controller
       JToolBarHelper::publish(string $task, string $alt, boolean $check); // Will call the task/function $task in your controller
       JToolBarHelper::unpublishList(); // Will call the task/function "unpublish" in your controller
       JToolBarHelper::unpublish(string $task, string $alt, boolean $check); // Will call the task/function $task in your controller
       JToolBarHelper::preferences('your_component_xml_file', 'height');
       JToolBarHelper::preferences(string $component, integer $height, integer $width, string $alt, string $path);
</source>
</source>
So (i.e.) your code would look like this:
 
*string <tt>$title</tt> {{rarr}} The title.</br>
*string <tt>$msg</tt> {{rarr}} Postscript for the 'are you sure' message.</br>
*string <tt>$task</tt> {{rarr}} An override for the task: 'controller.function'.</br>
*string <tt>$alt</tt> {{rarr}} An override for the alt text.</br>
*boolean <tt>$check</tt> {{rarr}} True if required to check that a standard list item is checked.</br>
*string <tt>$component</tt> {{rarr}} The name of the component, eg, com_content.</br>
 
<translate><!--T:3-->
So (i.e.) your code would look like this:</translate>
 
<source lang="php">
<source lang="php">
  class HelloViewHellos extends JView
  class HelloViewHellos extends JView
  {
  {
   function display($tpl = null) {
   function display($tpl = null) {
      global $mainframe, $option;
       JToolBarHelper::title( 'Hello Component', 'generic.png' );
       JToolBarHelper::title( 'Hello Component', 'generic.png' );
       JToolBarHelper::deleteList();
       JToolBarHelper::deleteList('hellos.delete');
       JToolBarHelper::editListX();
       JToolBarHelper::editList('hellos.edit');
       JToolBarHelper::addNewX();
       JToolBarHelper::addNew('hellos.add');
       JToolBarHelper::publishList();
       JToolBarHelper::publish();
       JToolBarHelper::unpublishList();
       JToolBarHelper::unpublish();
       JToolBarHelper::preferences('com_hello', '500');
       JToolBarHelper::preferences('com_hello', '500');
</source>
</source>
An example of creating a custom button in the admin list of records of your component {{JVer|3.x}} could be:


<translate><!--T:4-->
An example of creating a custom button in the admin list of records of your component {{JVer|3.x}} could be:</translate>
<tt>administrator/views/hellos/view.html.php</tt>


administrator/views/hellos/view.html.php
<source lang="php">
<source lang="php">
public function display($tpl = null)
public function display($tpl = null)
Line 61: Line 74:
</source>
</source>


administrator/controllers/hellos.php
<tt>administrator/controllers/hellos.php</tt>
 
<source lang="php">
<source lang="php">
     public function extrahello()
     public function extrahello()
Line 85: Line 99:
</source>
</source>


administrator/models/hello.php (note: the singular model)
<tt>administrator/models/hello.php</tt> <translate><!--T:5-->
(note: the singular model)</translate>
 
<source lang="php">
<source lang="php">
     public function extrahello($pks)
     public function extrahello($pks)
Line 98: Line 114:
</source>
</source>


Helpful explanation of JToolBarHelper/custom here - http://docs.joomla.org/JToolBarHelper/custom
<translate><!--T:6-->
Helpful explanation of JToolBarHelper/custom here</translate> - http://docs.joomla.org/JToolBarHelper/custom


<noinclude>[[Category:Component Development]] [[Category:Tutorials]] </noinclude>
<noinclude>
<translate>
<!--T:7-->
[[Category:Component Development]]
[[Category:Tutorials]]
</translate>
</noinclude>

Latest revision as of 12:47, 10 April 2017

Joomla! 
3.x

Note: This applies when using the MVC structure.

By default the Joomla Toolbar uses a Delete, Edit, New, Publish, Unpublish and Parameters buttons. To use them in your component you must write the following code in the view.html.php of your view file (below your display function).

      JToolBarHelper::title(string $title, string $icon);
      JToolBarHelper::deleteList(string  $msg, string  $task, string  $alt); // Will call the task/function $task in your controller
      JToolBarHelper::editList(string  $task, string  $alt); // Will call the task/function $task in your controller
      JToolBarHelper::addNew(string $task, string $alt, boolean  $check); // Will call the task/function $task in your controller
      JToolBarHelper::publish(string $task, string $alt, boolean $check); // Will call the task/function $task in your controller
      JToolBarHelper::unpublish(string $task, string $alt, boolean $check); // Will call the task/function $task in your controller
      JToolBarHelper::preferences(string $component, integer $height, integer $width, string $alt, string $path);
  • string $title    The title.
  • string $msg    Postscript for the 'are you sure' message.
  • string $task    An override for the task: 'controller.function'.
  • string $alt    An override for the alt text.
  • boolean $check    True if required to check that a standard list item is checked.
  • string $component    The name of the component, eg, com_content.

So (i.e.) your code would look like this:

 class HelloViewHellos extends JView
 {
   function display($tpl = null) {
      JToolBarHelper::title( 'Hello Component', 'generic.png' );
      JToolBarHelper::deleteList('hellos.delete');
      JToolBarHelper::editList('hellos.edit');
      JToolBarHelper::addNew('hellos.add');
      JToolBarHelper::publish();
      JToolBarHelper::unpublish();
      JToolBarHelper::preferences('com_hello', '500');

An example of creating a custom button in the admin list of records of your component Joomla 3.x could be: administrator/views/hellos/view.html.php

	public function display($tpl = null)
	{

		$this->state		= $this->get('State');
		$this->items		= $this->get('Items');
		$this->pagination	= $this->get('Pagination');

		// Check for errors.
		if (count($errors = $this->get('Errors'))) {
			JError::raiseError(500, implode("\n", $errors));
			return false;
		}

		$this->addToolbar();
        
        	$this->sidebar = JHtmlSidebar::render();
		parent::display($tpl);

	}

	protected function addToolbar()
	{
		// assuming you have other toolbar buttons ...

		JToolBarHelper::custom('hellos.extrahello', 'extrahello.png', 'extrahello_f2.png', 'Extra Hello', true);

	}

administrator/controllers/hellos.php

    public function extrahello()
	{

		// Get the input
		$input = JFactory::getApplication()->input;
		$pks = $input->post->get('cid', array(), 'array');

		// Sanitize the input
		JArrayHelper::toInteger($pks);

		// Get the model
		$model = $this->getModel();

		$return = $model->extrahello($pks);

		// Redirect to the list screen.
		$this->setRedirect(JRoute::_('index.php?option=com_hello&view=hellos', false));

	}

administrator/models/hello.php (note: the singular model)

    public function extrahello($pks)
	{

		// perform whatever you want on each item checked in the list

		return true;

	}

Helpful explanation of JToolBarHelper/custom here - http://docs.joomla.org/JToolBarHelper/custom