ADD MVC for part

This commit is contained in:
Thomas Kuschel 2023-10-05 02:28:17 +02:00
parent 35c19b24e3
commit f0b8e1ed02
6 changed files with 238 additions and 1 deletions

145
README.md
View File

@ -283,3 +283,148 @@ Methods in the table objects are:
1. **delete()** to delete the record from the database. 1. **delete()** to delete the record from the database.
--- ---
### Get a Form in Joomla component
The **Form** class of Joomla is used to create complex forms with flexible
layouts and dynamic properties. First, the form fields are defined in the
XML file. Then the view file gets the form from the model and layout file
displays the form.
#### XML Form file
**admin/forms/part.xml**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<form>
<field
name="title"
type="text"
label="JGLOBAL_TITLE"
required="true"
/>
</form>
```
Similarly you can add other fields to the XML file.
#### View file
**admin/src/View/Part/HtmlView.php**
This file is similar to the view file added earlier for the "Parts" view.
The view file gets the form from the model in the display() method.
```php
<?php
namespace KW4NZ\Component\Depot\Administrator\View\Part;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
\defined('_JEDEC') or die;
class HtmlView extends BaseHtmlView
{
protected $form;
protected $item;
public function display($tpl = null)
{
$this->form = $this->get('Form');
$this->item = $this->get('Item');
parent::display($tpl);
}
}
```
The code will look for the method **getForm()**
and **getItem()** in the model file.
We need to get the item to get the ID
of the record in case of editing the existing record.
#### Model file
**admin/src/Model/PartModel.php**
We need to extend the model class with the **AdminModel**.
The AdminModel class extends FormModel class. The **getForm()**
method gets the Form object for the edit form.
```php
namespace KW4NZ\Component\Depot\Administrator\Model;
use Joomla\CMS\MVC\Model\AdminModel;
\defined('_JEXEC') or die;
class PartModel extends AdminModel
{
public function getForm($data = array(), $loadData = true)
{
$form = $this->loadForm('com_depot.part',
'part', array('control' => 'jform',
'load_data' => $loadData));
if (empty($form)) {
return false;
}
return $form;
}
}
```
The **loadForm()** and **preprocessForm()** methods are defined in
the FromModel class and the **bind()** method is defined in the
Form class. The first argument (name) of loadForm() is set to
"com_depot.part". The second argument (form xml source) is "part",
and the third argument is the associative array for options.
The form is defined in the source file: **forms/part.xml**
After you have set the $form variable with the Form object,
you check to see if you are loading data to the form.
If you want to pre-load data for the form, you include an element
called "load_data" that is set to a boolean true.
Then, the method calls the loadFormData() method to get the data
for the form. This method gets any previously loaded data from
the session or database.
###### Modifying Form dynamically
Inside the getForm() method, before returning the $form, you can modify the form with many methods of the Form class. You can easily fine-tune your forms dynamically before they are rendered.
#### Layout file - rendering Form
**tmpl/part/edit.php**
After you get the form in the view file ($this->form), the form
is rendered in the layout file (edit.php).
```php
<?php
use Joomla\CMS\HTML\HTMLHelper;
$wa = $this->document->getWebAssetManager();
$wa->useScript('keepalive');
$wa->useScript('form.validate');
?>
<form action="<?= Route::_('index.php?option=com_depot&layout=edit&id=' .
(int) $this->item->id); ?>"
method="post" name="adminForm" id="item-form" class="form-validate">
<?= $this->form->renderField('title'); ?>
<input type="hidden" name="task" value="part.edit" />
<?= HTMLHelper::_('form.token'); ?>
</form>
```
The form validate script is required to submit the for. The renderField() method
of the Form class displays the field - both label and input.
We can access the form using following URL:
```php
administrator/index.php?option=com_depot&view=part&layout=edit
```
It displays edit.php layout file of the Part View. When the form is submitted, the
data is sent to the controller depending upon the action buttons in the toolbar.
Please remember to add the additional folder `form` to the Manifest file "depot.xml", i.e.
just a line before `<folder>services</folder>` :
```xml
<folder>form</folder>
```
---

9
admin/form/part.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<form>
<field
name="component_name"
type="text"
label="JFIELD_NAME_LABEL"
required="true"
/>
</form>

View File

@ -0,0 +1,30 @@
<?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.3
*/
namespace KW4NZ\Component\Depot\Administrator\Model;
use Joomla\CMS\MVC\Model\AdminModel;
\defined('_JEXEC') or die;
class PartModel extends AdminModel
{
public function getForm($data = array(), $loadData = true)
{
$form = $this->loadForm('com_depot.part',
'part', array('control' => 'jform',
'load_data' => $loadData));
if (empty($form)) {
return false;
}
return $form;
}
}

View File

@ -0,0 +1,28 @@
<?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.3
*/
namespace KW4NZ\Component\Depot\Administrator\View\Part;
use Joomla\CMS\MVC\View\HtmlView as BaseHtmlView;
\defined('_JEDEC') or die;
class HtmlView extends BaseHtmlView
{
protected $form;
protected $item;
public function display($tpl = null)
{
$this->form = $this->get('Form');
$this->item = $this->get('Item');
parent::display($tpl);
}
}

24
admin/tmpl/part/edit.php Normal file
View File

@ -0,0 +1,24 @@
<?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.3
*/
use Joomla\CMS\HTML\HTMLHelper;
$wa = $this->document->getWebAssetManager();
$wa->useScript('keepalive');
$wa->useScript('form.validate');
?>
<form action="<?= Route::_('index.php?option=com_depot&layout=edit&id=' . (int) $this->item->id); ?>"
method="post" name="adminForm" id="item-form" class="form-validate">
<?= $this->form->renderField('title'); ?>
<input type="hidden" name="task" value="part.edit" />
<?= HTMLHelper::_('form.token'); ?>
</form>

View File

@ -7,7 +7,7 @@
<license>GPL v2 +; see LICENSE.md</license> <license>GPL v2 +; see LICENSE.md</license>
<authorEmail>thomas@kuschel.at</authorEmail> <authorEmail>thomas@kuschel.at</authorEmail>
<authorUrl>https://kuschel.at</authorUrl> <authorUrl>https://kuschel.at</authorUrl>
<version>0.0.2</version> <version>0.0.3</version>
<description>COM_DEPOT_XML_DESCRIPTION</description> <description>COM_DEPOT_XML_DESCRIPTION</description>
<namespace path="src/">KW4NZ\Component\Depot</namespace> <namespace path="src/">KW4NZ\Component\Depot</namespace>
<install> <!-- Runs on install --> <install> <!-- Runs on install -->
@ -55,6 +55,7 @@
</menu> </menu>
</submenu> </submenu>
<files folder="admin"> <files folder="admin">
<folder>form</folder>
<folder>services</folder> <folder>services</folder>
<folder>sql</folder> <folder>sql</folder>
<folder>src</folder> <folder>src</folder>