In Joomla, the administrator interacts with components through a toolbar. Toolbar is a collection of action buttons. It also creates a title for the component.
There are lot of backend action buttons that you can add:
- Media Manager
- Save and New
- Save and Copy
You need to add following code in the function display in view.html.php
// Set the toolbar
The toolbar should be added before displaying the template.
Next, add the page title and toolbar using the class JToolbarHelper.
protected function addToolBar()
JToolbarHelper::publish('helloworlds.publish', 'JTOOLBAR_PUBLISH', true);
JToolbarHelper::unpublish('helloworlds.unpublish', 'JTOOLBAR_UNPUBLISH', true);
If the action can be performed on more than one record (like for deleting or publishing), then helloworlds is used. Otherwise, if the action can be performed only on one record (like adding new or editing), then helloworld is used.
Every action is associated with one task (add, edit, delete). Following code is added to the layout file of the view located at tmpl/default.php
<input type="hidden" name="task" value=""/>
<input type="hidden" name="boxchecked" value="0"/>
<?php echo JHtml::_('form.token'); ?>
The "task" field is used to define the controller.task parameter which is sent to the server in the POST request when the form is submitted. The "boxchecked" field is used to keep track of the number of checkboxes ticked. Using the form token helps prevent CSRF attacks.
How It Works
First, the actions are added in the toolbar:
These are compound tasks (controller.task). The first part is the controller. Accordingly, Joomla will look in the file: admin/controllers/helloworlds.php or admin/controllers/helloworld.php
Controller classes are located at two places: the root folder and the controllers folder. In the root folder there is a file named 'controller.php', this is the master controller of the component. It is a direct subclass of JControllerLegacy.
In the controllers folder, there are many files named according to entities in the component. These controllers subclass either JControllerAdmin or JControllerForm. JControllerAdmin and JControllerForm are subclasses of JControllerLegacy. Controllers defined in controllers folder as called subcontroller.
Master Controller and Sub Controllers
The master controller found in the root folder is only responsible for the display task. This is the default task for JControllerLegacy. JControllerLegacy uses the task variable (from the request) to determine what Controller class to load. If the task variable contains a dot, it assumes that this variable is in the form of controller.task. The task variable specifies method of controller to run. It will load controller class in controllers folder and rewrite task variable to real task.
If the task does not contain a dot, JControllerLegacy will load the master controller located in the root folder. In the master controller, a default view is specified. If there is no view variable in the URL request, this one is used.
- To use the master controller, specify a view variable as view name to use or the default one will be used.
- To use a subcontroller, specify the task variable in the form of controller.task
For example, helloworld.edit is the task variable to edit a record. This will cause the helloworld subcontroller to be loaded and the edit method to be executed.
How Master Controller Works
There are two things: view and task. Depending on the URL, following can happen:
- If a view name is not defined in URL, default view is used and a default task (display) is executed. This is used to display a list of records. For example: index.php?option=com_content
- If a view is defined but the task is not, master controller loads specified view and executes display method. This is also used to display a list of records. For example: index.php?option=com_content&view=articles
- If a view is specified with a task (but not in the dot format), master controller loads the specified view and executes method specified by task. This case is used to display edit form, or to present one item of record to user. For example: index.php?option=com_content&view=article&layout=edit
When the master controller creates a view, it normally loads a model having the same name as the view and pushes model into the view. Then the view's display method is called. In view, you get data from model using function like $this->get("Items"). In this case, view will look for method getItems in model and execute it.
Subcontrollers handle all CRUD (Create, Read, Update, Delete) tasks.
For tasks that do not need a view (such as save, delete, publish), the subcontroller just deletes or updates records and redirects the user back to list view. In this case, the user must select at least one item of record, and usually the name of the controller is given in plural form such as "helloworlds.delete". This subcontroller is generally inherited from JControllerAdmin which suppress display method by default.
Delete, Publish and Unpublish Actions
The tasks for these actions are:
Joomla will look in the file: admin/controllers/helloworlds.php
These functions are already defined in the class JControllerAdmin, so you are not required to define them again. Just extend the Controller class with JControllerAdmin in admin/controllers/helloworlds.php
class HelloWorldControllerHelloWorlds extends JControllerAdmin
* Proxy for getModel.
public function getModel($name = 'HelloWorld', $prefix = 'HelloWorldModel', $config = array('ignore_request' => true))
$model = parent::getModel($name, $prefix, $config);
Extend the Model class with JModelAdmin in admin/models/helloworld.php
class HelloWorldModelHelloWorld extends JModelAdmin
* Method to get a table object, load it if necessary.
public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array())
return JTable::getInstance($type, $prefix, $config);
New and Edit Actions
The tasks for these actions are:
Joomla will look in the file: admin/controllers/helloworld.php. These functions are already defined in the class JControllerForm, so you are not required to define them again. Just extend the Controller class with JControllerForm in admin/controllers/helloworld.php
class HelloWorldControllerHelloWorld extends JControllerForm
Add and edit are redirected to same editing form view.
Joomla will look into admin/views/helloworld/view.html.php. It will get the data from the model, check for errors, set the toolbar and display the template using layout in tmpl/edit.php
The form is obtained in the model. You have to define the form in XML in the folder: admin/models/forms/helloworld.xml
Then, you have to write function getForm in the Model class. This function will call another function loadForm, that processes the XML to produce a JForm object. This method is defined in both the classes JModelAdmin and JModelForm.
Optionally, you can write loadFormData() to provide prefill data for the form.