I would like some help in using the delete() methods of the AdminController and AdminModel when in a front end list view to avoid duplicating code unnecessarily.
I have a listing of records in a frontend view for my component and I have added a checkbox for each line and a toolbar button to delete selected records. The delete button works in that the records selected in the view are available to the subcontroller via $this->input->get('cid', array(), 'array');
I have followed examples of how to put a method in my subcontroller that did enough to delete the records but some of the examples were more or less a cut and paste of the delete() methods in the AdminController and AdminModel. I would like to follow DRY principles and reduce my front in code where possible therefore I have been hacking away trying to invoke these Admin methods directly.
Links to the two classes I would like to use.
https://github.com/joomla/joomla-cms/blob/staging/libraries/src/MVC/Controller/AdminController.php
starting on line 111. and
https://github.com/joomla/joomla-cms/blob/staging/libraries/src/MVC/Model/AdminModel.php
starting on line 820.
Following the comment by Robbie Jackson I have updated the sample code to show what almost works.
In .../views/items/view.html.php I have this line for the toolbar button.
ToolbarHelper::deleteList('COM_INVENTORY_DELETE_CONFIRM','item.delete', 'COM_INVENTORY_DELETE_CONFIRM');
In .../model/item.php I now have
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\AdminModel;
use Joomla\CMS\Table\Table;
...
class InventoryModelItem extends AdminModel
In .../controller/item.php I have
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\AdminController;
use ...
/**
* Item controller class.
*/
class InventoryControllerItem extends AdminController
{
.... unrelated methods ....
public function delete()
{
...
$model = $this->getModel('Item', 'InventoryModel');
$cid = ArrayHelper::toInteger($cid);
if ($model->delete($cid))
...
}
As well as extending the AdminController and AdminModel I had to replace $model = $this->getModel();
with $model = $this->getModel('Item', 'InventoryModel');
in my delete() method of the subcontroller to make it work. Without it $model was empty and it would fail on $model->delete($cid)
.
So while it can work, it still requires me to cut and paste the whole delete() method from AdminController just to specify my model, item, in item.php which is the default model.
There is something I am missing to make $this->model()
find my model without me having to specify it implicitly.
Even any reference to help show how to use more Admin functionality in the front end would be appreciated, it sometimes feel like two different worlds. :)