diff --git a/README.md b/README.md index 453dca7..760aec2 100644 --- a/README.md +++ b/README.md @@ -439,3 +439,90 @@ just a line before `services` : ``` --- + +### 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()`. + +--- diff --git a/admin/src/Controller/PartController.php b/admin/src/Controller/PartController.php new file mode 100644 index 0000000..d3a3dd7 --- /dev/null +++ b/admin/src/Controller/PartController.php @@ -0,0 +1,19 @@ + + * @copyright (C) 2023 KW4NZ, + * @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 +{ +} \ No newline at end of file diff --git a/admin/src/Model/PartModel.php b/admin/src/Model/PartModel.php index 17f4d22..944c6bd 100644 --- a/admin/src/Model/PartModel.php +++ b/admin/src/Model/PartModel.php @@ -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; + } } \ No newline at end of file diff --git a/admin/src/View/Part/HtmlView.php b/admin/src/View/Part/HtmlView.php index 4c82fcc..e856722 100644 --- a/admin/src/View/Part/HtmlView.php +++ b/admin/src/View/Part/HtmlView.php @@ -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'); + } } \ No newline at end of file diff --git a/admin/tmpl/part/edit.php b/admin/tmpl/part/edit.php index 6039c38..3fc2f28 100644 --- a/admin/tmpl/part/edit.php +++ b/admin/tmpl/part/edit.php @@ -16,11 +16,12 @@ $wa->useScript('form.validate') ->useScript('keepalive'); ?> -
- form->renderField('component_name'); ?> + form->renderField('component_name'); ?> + form->renderField('id'); ?> - +
\ No newline at end of file diff --git a/admin/tmpl/parts/default.php b/admin/tmpl/parts/default.php index cd610e7..a0189e1 100644 --- a/admin/tmpl/parts/default.php +++ b/admin/tmpl/parts/default.php @@ -10,4 +10,4 @@ ?>

Welcome to my Depot Component!

-

Link: Part

\ No newline at end of file +

Link: Part

\ No newline at end of file diff --git a/depot.xml b/depot.xml index 445fe24..90878b0 100644 --- a/depot.xml +++ b/depot.xml @@ -7,7 +7,7 @@ GPL v2 +; see LICENSE.md thomas@kuschel.at https://kuschel.at - 0.0.3 + 0.0.4 COM_DEPOT_XML_DESCRIPTION KW4NZ\Component\Depot