ADD B4 Actions: Save and View

This commit is contained in:
Thomas Kuschel 2023-10-07 23:33:29 +02:00
parent 9db4ad808b
commit 436734cd4c
7 changed files with 144 additions and 9 deletions

View File

@ -439,3 +439,90 @@ just a line before `<folder>services</folder>` :
```
---
### Adding administrator's actions (Back-end) - Save and Cancel
In the form View, you can add action buttons like "Save" and "Cancel" to submit the form
or cancel the editing respectively. These buttons are added to the toolbar.
These buttons require compound tasks (controller and method). For example,
- Save and Edit: part.apply
- Save and Close: part.save
- Cancel: part.cancel
#### View file
**admin/src/View/Part/HtmlView.php**
In the View file, we create a new method to add a toolbar: `addToolbar()`. The
toolbar hides the sidebar menu on forms, sets the title and adds action buttons.
```php
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;
protected function addToolbar()
{
Factory::getApplication()->getInput()->set('hidemainmenu', true);
ToolbarHelper::title('Part: Add');
ToolbarHelper::apply('part.apply');
ToolbarHelper::save('part.save');
ToolbarHelper::cancel('part.cancel', 'JTOOLBAR_CLOSE');
}
```
The display() calls this method to include the toolbar.
```php
public function display($tpl = null)
{
$this->form = $this->get('Form');
$this->addToolbar();
parent::display($tpl);
}
```
#### Controller file
**admin/src/Controller/PartController.php***
Now, when these action buttons are clicked, Joomla will look for apply(), save() or cancel()
methods in the Part controller. So, create a controller clss that will extend FormController.
These methods are already defined in the parent class.
```php
use Joomla\CMS\MVC\Controller\FormController;
class PartController extends FormController
{
}
```
#### Model file
**admin/src/model/PartModel.php**
If you click on Save button, it will save the data and redirect to the editing screen. However, the data will not be present in the form. Though, we have set `$loadData` to true, we also need to create a method `loadFormData()` to get the data for the form.
```php
protected function loadFormData()
{
$app = Factory::getApplication();
$data = $app->getUserState('com_depot.edit.part.data', []);
if (empty($data)) {
$data = $this->getItem();
}
return $data;
}
```
First, this method tries to get the data from the session. If it fails to get data from the
session, then it gets data from the database. The `getItem()` method is defined in the
parent class.
Suppose the user has filled out the form to add a new item but has some invalid data in the
form. In this case, the save will not be successful, so the data is not saved in the
database. It would be very frustrating if the user hat to reenter all the fields to fix a
single error. To handle this scenario, you save the entered data in the user's session.
Only after the save is successful, you clear this out of the session.
So, you either have data from the database, which you can get with `getItem()`, or you have
data from the session, which you can get with `getUserSate()`.
---

View File

@ -0,0 +1,19 @@
<?php
/**
* @package Depot.Administrator
* @subpackage com_depot
* @author Thomas Kuschel <thomas@kuschel.at>
* @copyright (C) 2023 KW4NZ, <https://www.kuschel.at>
* @license GNU General Public License version 2 or later; see LICENSE.md
* @since 0.0.4
*/
namespace KW4NZ\Component\Depot\Administrator\Controller;
use Joomla\CMS\MVC\Controller\FormController;
defined('_JEXEC') or die;
class PartController extends FormController
{
}

View File

@ -10,6 +10,7 @@
namespace KW4NZ\Component\Depot\Administrator\Model;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\AdminModel;
\defined('_JEXEC') or die;
@ -26,4 +27,16 @@ class PartModel extends AdminModel
return $form;
}
protected function loadFormData()
{
$app = Factory::getApplication();
$data = $app->getUserState('com_depot.edit.part.data', []);
if (empty($data)) {
$data = $this->getItem();
}
return $data;
}
}

View File

@ -10,10 +10,13 @@
namespace KW4NZ\Component\Depot\Administrator\View\Part;
defined('_JEXEC') or die;
use Joomla\CMS\Factory;
// use Joomla\CMS\MVC\View\GenericDataException;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
use Joomla\CMS\Toolbar\Toolbar;
use Joomla\CMS\Toolbar\ToolbarHelper;
\defined('_JEXEC') or die;
/**
* View to edit an article.
@ -43,8 +46,8 @@ class HtmlView extends BaseHtmlView
*
* @return mixed A string if successful, otherwise an Error object.
*
* @throws \Exception
* @since 1.6
* @throws \Exception
* @since 1.6
*/
public function display($tpl = null)
{
@ -54,7 +57,19 @@ class HtmlView extends BaseHtmlView
// if (count($errors = $this->get('Errors'))) {
// throw new GenericDataException(implode("\n", $errors), 500);
// }
$this->addToolbar();
return parent::display($tpl);
}
protected function addToolbar()
{
Factory::getApplication()->getInput()->set('hidemainmenu', true);
ToolbarHelper::title('Part: Add');
ToolbarHelper::apply('part.apply');
ToolbarHelper::save('part.save');
ToolbarHelper::cancel('part.cancel', 'JTOOLBAR_CLOSE');
}
}

View File

@ -16,11 +16,12 @@ $wa->useScript('form.validate')
->useScript('keepalive');
?>
<form action="<?php echo Route::_('index.php?option=com_depot&view=part&layout=edit&id=' . (int) $this->item->id); ?>"
<form action="<?= Route::_('index.php?option=com_depot&view=part&layout=edit&id=' . (int) $this->item->id); ?>"
method="post" name="adminForm" id="item-form" class="form-validate">
<?php echo $this->form->renderField('component_name'); ?>
<?= $this->form->renderField('component_name'); ?>
<?= $this->form->renderField('id'); ?>
<input type="hidden" name="task" value="part.edit" />
<?php echo HTMLHelper::_('form.token'); ?>
<?= HTMLHelper::_('form.token'); ?>
</form>

View File

@ -10,4 +10,4 @@
?>
<h2>Welcome to my Depot Component!</h2>
<p>Link: <a href="index.php?option=com_depot&view=part&layout=edit">Part</a></p>
<p>Link: <a href="index.php?option=com_depot&view=part&layout=edit&id=1">Part</a></p>

View File

@ -7,7 +7,7 @@
<license>GPL v2 +; see LICENSE.md</license>
<authorEmail>thomas@kuschel.at</authorEmail>
<authorUrl>https://kuschel.at</authorUrl>
<version>0.0.3</version>
<version>0.0.4</version>
<description>COM_DEPOT_XML_DESCRIPTION</description>
<namespace path="src/">KW4NZ\Component\Depot</namespace>
<install> <!-- Runs on install -->