Creating a toolbar for your component: Difference between revisions

From Joomla! Documentation

MATsxm (talk | contribs)
Typo
Some functions were deprecated
Line 5: Line 5:


<translate><!--T:2-->
<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>
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). Note that this functions will create a different type of button, but what they do must be in the arguments Ex:"controller.function".</translate>
<source lang="php">
<source lang="php">
       JToolBarHelper::title( 'your_component_view_page_title', 'generic.png' );
       JToolBarHelper::title( 'your_component_view_page_title', 'generic.png' );
       JToolBarHelper::deleteList(); // Will call the task/function "remove" in your controller
       JToolBarHelper::deleteList(string); // Will call the task/function "remove" in your controller
       JToolBarHelper::editListX(); // Will call the task/function "edit" in your controller
       JToolBarHelper::editList(string); // Will call the task/function "edit" in your controller
       JToolBarHelper::addNewX(); // Will call the task/function "add" in your controller
       JToolBarHelper::addNew(string); // Will call the task/function "add" in your controller
       JToolBarHelper::publishList(); // Will call the task/function "publish" in your controller
       JToolBarHelper::publishList(); // Will call the task/function "publish" in your controller
       JToolBarHelper::unpublishList(); // Will call the task/function "unpublish" in your controller
       JToolBarHelper::unpublishList(); // Will call the task/function "unpublish" in your controller

Revision as of 18:23, 9 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). Note that this functions will create a different type of button, but what they do must be in the arguments Ex:"controller.function".

      JToolBarHelper::title( 'your_component_view_page_title', 'generic.png' );
      JToolBarHelper::deleteList(string); // Will call the task/function "remove" in your controller
      JToolBarHelper::editList(string); // Will call the task/function "edit" in your controller
      JToolBarHelper::addNew(string); // Will call the task/function "add" in your controller
      JToolBarHelper::publishList(); // Will call the task/function "publish" in your controller
      JToolBarHelper::unpublishList(); // Will call the task/function "unpublish" in your controller
      JToolBarHelper::preferences('your_component_xml_file', 'height');

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();
      JToolBarHelper::editListX();
      JToolBarHelper::addNewX();
      JToolBarHelper::publishList();
      JToolBarHelper::unpublishList();
      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