Categories
CMS Development Joomla

In front end of component use the Admin controller and model to delete records

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.

I have been going around in circles trying things but I will try and explain it with examples as best I can.

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 …/controller/item.php I have

use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\Utilities\ArrayHelper;


/**
 * Item controller class.
 */
class InventoryControllerItem extends BaseController
{
    .... unrelated methods ....

    public function delete()
    {
         A cut an paste of delete() method from AdminController.php where I have
         tried to use JLoader::register('AdminModel', JPATH_ADMINISTRATOR . '/libraries/src/MVC/Model/AdminModel.php');
          and
          $model = BaseDatabaseModel::getInstance(,'AdminModel');
          prior to the
          $model = $this->getModel();
     }

What I would like to have if is possible is something like

public function delete()
{
   load/register whatever it is I need to do for AdminController.php;
   load/register whatever it is I need to do for AdminModel.php;

   call admin version of delete() method

   return
}

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.

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. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *