Add Backend Toolbar and Actions in Joomla

In Joomla!, the administrator interacts with components through a toolbar. Toolbar is a collection of action buttons (New, Save, Close, Publish and so on). It also creates a title for the component.

Toolbar Buttons

There are many types of toolbar buttons. Each button has option accessors like name, text, task, icon, buttonClass, listCheck and so on.

  1. Link Button: url, target.
  2. Popup Button: url, iframeWidth, iframeHeight, bodyHeight, modalWidth, onclose, title, footer, selector.
  3. Custom Button: html.
  4. Help Button: ref, useComponent, component, url.
  5. Dropdown Button: toggleSplit, toggleButtonClass.
  6. Confirm Button: message

Namespace

use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;

First, get the toolbar object instance.

$toolbar = Toolbar::getInstance('toolbar');

Standard Buttons

These are Joomla standard buttons: apply, new, save, save-new, save-copy, save-close, featured, publish, unpublish, cancel, trash, delete.

1. Add New Button

To create a button for adding a new record.

$toolbar->addNew('planet.add');

2. Dropdown Button

To create a drop down button, first create a dropdown. Then, add child buttons. 

$dropdown = $toolbar->dropdownButton('status-group')
->text('JTOOLBAR_CHANGE_STATUS')
->toggleSplit(false)
->icon('icon-ellipsis-h')
->buttonClass('btn btn-action')
->listCheck(true); 

$childBar = $dropdown->getChildToolbar();

$childBar->publish('planets.publish')->listCheck(true);

$childBar->unpublish('planets.unpublish')->listCheck(true);

$childBar->standardButton('featured')
->text('JFEATURE')
->task('planets.featured')
->listCheck(true);

$childBar->standardButton('circle')
->text('JUNFEATURE')
->task('planets.unfeatured')
->listCheck(true);

$childBar->archive('planets.archive')->listCheck(true);

$childBar->checkin('planets.checkin')->listCheck(true);

$childBar->trash('planets.trash')->listCheck(true);

$childBar->popupButton('batch')
->text('JTOOLBAR_BATCH')
->selector('collapseModal')
->listCheck(true);

The listCheck(true) method activates the button when one or more items are selected from the list. The icon() method sets the icon for the button.