Adding Back-end Actions: Delete

From the list of records layouts, you can add a toolbar button to delete records. The task for the Delete action is: planets.delete

Joomla! will look for the delete() method in the controller file: src/Controller/PlanetsController.php

The delete() method is defined in the parent class AdminController, so you are not required to define it again. Extend the Controller class with AdminController.

This method gets the ids from the post request. Then, it gets the model and on that model, define a function delete() and pass the ids. Then, it sets up a message for successful deletion and finally set the redirect to the list of items. In the model, the delete() gets the table and delete the records with ids.

Publish and Unpublish actions work in the similar way.

Step 1: Controller File

src/Controller/PlanetsController.php

The controller "Planets" uses "Planet" model because we need the functionality of AdminModel (used by Planet model) and not the functionality of ListModel (used by Planets model). So, first you need to get this model.

use Joomla\CMS\MVC\Controller\AdminController;

class PlanetsController extends AdminController
{
    public function getModel($name = 'Planet', $prefix = 'Administrator', $config = array('ignore_request' => true))
    {
        return parent::getModel($name, $prefix, $config);
    }
}

Step 2: Model File

src/Model/PlanetModel.php

Extend the Model class with AdminModel

Here, you need to get the table.

class PlanetModel extends AdminModel
{
public function getTable($name = 'Planet', $prefix = 'Table', $options = array())
{
 if ($table = $this->_createTable($name, $prefix, $options))
{
  return $table;
}
}
}

However, if the table name and model name name are same (Planet in this case), you are not required to define the method.