Adding Back-end Actions: New and Edit
Add and Edit actions both redirect to the same form. You can display the Edit by clicking the title. If the id is 0, it indicates Add action. If the id is greater than 0, it indicates Edit action.
The tasks for these actions are:
- planet.edit
- planet.add
Joomla! will look for these methods in the Controller file: src/Controller/PlanetController.php
These methods, add() and edit(), are already defined in the parent class FormController, so you are not required to define them again. The add() method sets the redirect to the editing form layout with the id = 0. The edit() gets the id and then sets the redirect to the editing form layout.
Step 1: Edit View File
src/View/Planets/HtmlView.php
Add the following code in the display() method.
// Set the toolbar
$this->addToolbar();
The toolbar should be added before displaying the template.
Step 2: Create New Method
src/View/Planets/HtmlView.php
Next, add the page title and toolbar using the class ToolbarHelper.
protected function addToolbar()
{
ToolbarHelper::title(Text::_('COM_PLANETS_MANAGER_PLANETS'));
ToolbarHelper::addNew('planet.add');
ToolbarHelper::deleteList('JGLOBAL_CONFIRM_DELETE', 'planets.delete');
ToolbarHelper::publish('planets.publish', 'JTOOLBAR_PUBLISH', true);
ToolbarHelper::unpublish('planets.unpublish', 'JTOOLBAR_UNPUBLISH', true);
}
If the action can be performed on more than one record (like deleting or publishing records), then planets (plural form) is used. Otherwise, if the action can be performed only on one record (like adding new record), then planet (singular form) is used.
Step 3: Edit Layout File
tmpl/planets/default.php
Every action is associated with one task (add, publish, delete). Following code is added to the layout file:
<input type="hidden" name="task" value=""/>
<input type="hidden" name="boxchecked" value="0"/>
<?php echo HTMLHelper::_('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. The form token helps to prevent CSRF attacks.
Compound Tasks
First, the actions are added to the toolbar:
- planets.delete
- planets.publish
- planets.unpublish
- planets.add
These are compound tasks (controller.task).
Controller
The first part is the controller. The Controller classes are located at src/Controller. These controller classes extend either AdminController or FormController. AdminController and FormController are subclasses of BaseController.
- AdminController: For list of records
- FormController: For form
Delete, Publish and Unpublish Actions
The tasks for these actions are:
- planets.delete
- planets.publish
- planets.unpublish
Step 4: Controller File
Joomla! will look in the file: src/Controller/PlanetsController.php
These methods are already defined in the parent class AdminController, so you are not required to define them again. Just extend the Controller class with the AdminController.
class PlanetsController extends AdminController
{
public function getModel($name = 'Planet', $prefix = 'Administrator', $config = array('ignore_request' => true))
{
return parent::getModel($name, $prefix, $config);
}
}
New and Edit Actions
The tasks for these actions are:
- planet.edit
- planet.add
These methods are already defined in the parent class FormController, so you are not required to define them again. Just extend the Controller class with the FormController.
src/Controller/PlanetController.php
namespace TechFry\Component\Planets\Administrator\Controller;
defined('_JEXEC') or die;
use Joomla\CMS\MVC\Controller\FormController;
class PlanetController extends FormController
{
}
Add and edit are redirected to same editing form view. The form is obtained from the model. You have to define the form in XML in the folder: admin/forms/planet.xml
After the form is saved, the record is added or updated in the database.
Step 5: List Layout File for Edit Link
tmpl/planets/default.php
First, change the title in the list of records layout to link it to the form.
<th>
<a href="/<?php echo Route::_('index.php?option=com_planets&task=planet.edit&id=' . $row->id); ?>" title="<?php echo Text::_('JACTION_EDIT'); ?> <?php echo $this->escape($row->title); ?>">
<?php echo $this->escape($row->title); ?>
</a>
</th>
Now, on clicking the title link, editing layout will be opened.