Create Controller Action Delete
Rsgitech\News\Controller\Adminhtml\Allnews\Delete.php
<?php
namespace Rsgitech\News\Controller\Adminhtml\Allnews;
class Delete extends \Magento\Backend\App\Action
{
/**
* Authorization level
*
* @see _isAllowed()
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Rsgitech_News::news_delete');
}
/**
* Delete action
*
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
// check if we know what should be deleted
$id = $this->getRequest()->getParam('news_id');
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
if ($id) {
$title = "";
try {
// init model and delete
$model = $this->_objectManager->create(\Rsgitech\News\Model\Allnews::class);
$model->load($id);
$title = $model->getTitle();
$model->delete();
// display success message
$this->messageManager->addSuccess(__('The news has been deleted.'));
// go to grid
$this->_eventManager->dispatch(
'adminhtml_news_on_delete',
['title' => $title, 'status' => 'success']
);
return $resultRedirect->setPath('*/*/');
} catch (\Exception $e) {
$this->_eventManager->dispatch(
'adminhtml_news_on_delete',
['title' => $title, 'status' => 'fail']
);
// display error message
$this->messageManager->addError($e->getMessage());
// go back to edit form
return $resultRedirect->setPath('*/*/edit', ['news_id' => $id]);
}
}
// display error message
$this->messageManager->addError(__('We can\'t find a news to delete.'));
// go to grid
return $resultRedirect->setPath('*/*/');
}
}
?>
Create Controller Action MassDelete
Rsgitech\News\Controller\Adminhtml\Allnews\MassDelete.php
<?php
namespace Rsgitech\News\Controller\Adminhtml\Allnews;
use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Rsgitech\News\Model\ResourceModel\Allnews\CollectionFactory;
/**
* Class MassDelete
*/
class MassDelete extends \Magento\Backend\App\Action
{
/**
* @var Filter
*/
protected $filter;
/**
* @var CollectionFactory
*/
protected $collectionFactory;
/**
* @param Context $context
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
{
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
parent::__construct($context);
}
/**
* Authorization level
*
* @see _isAllowed()
*/
protected function _isAllowed()
{
return $this->_authorization->isAllowed('Rsgitech_News::news_delete');
}
/**
* Execute action
*
* @return \Magento\Backend\Model\View\Result\Redirect
* @throws \Magento\Framework\Exception\LocalizedException|\Exception
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$collectionSize = $collection->getSize();
foreach ($collection as $news) {
$news->delete();
}
$this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $collectionSize));
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath('*/*/');
}
}
?>
Then run below commands
php bin/magento setup:upgrade
php bin/magetno setup:static-content:deploy
php bin/magento cache:flush
0 comments:
Post a Comment
Thanks