I have a module in which there are 2 custom grids and these two grids have the same crud functionality but different classes, but the inner logic is the same, how can I achieve this in a more optimized way?
Note: Both grids have their own table and model classes
is there a way I can perform crud functionality on these grids without creating separate classes for each crud operation?
This is the delete operation class, which is almost same in both scenario
// check if we know what should be deleted
$id = $this->getRequest()->getParam('id');
/** @var Redirect $resultRedirect */
$model = $this->modelFactory->create();
$resourceModel = $this->resourceModelFactory->create();
$resultRedirect = $this->resultRedirectFactory->create();
if ($id) {
try {
// init modelFactory and delete
$country = $resourceModel->load($model, $id);
$country->delete($model);
// display success message
$this->messageManager->addSuccessMessage(__('The item has been deleted.'));
return $resultRedirect->setPath('*/*/');
} catch (Exception $e) {
// display error message
$this->messageManager->addErrorMessage($e->getMessage());
// go back to edit form
return $resultRedirect->setPath('*/*/edit', ['id' => $id]);
}
}
// display error message
$this->messageManager->addErrorMessage(__('We can't find a item to delete.'));
// go to grid
return $resultRedirect->setPath('*/*/');
if there any question let me know?