Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace MageplazaHelloWorldControllerAdminhtmlPost;
- use MagentoBackendAppAction;
- class Delete extends MagentoBackendAppAction
- {
- protected $model;
- public function __construct(
- ActionContext $context,
- MageplazaHelloWorldModelDataProvider $model
- ) {
- $this->model = $model;
- parent::__construct($context);
- }
- public function execute()
- {
- // check if we know what should be deleted
- $id = $this->getRequest()->getParam('id');
- /** @var MagentoBackendModelViewResultRedirect $resultRedirect */
- $resultRedirect = $this->resultRedirectFactory->create();
- if ($id) {
- $title = "";
- try {
- $this->model->load($id);
- $title = $this->model->getTitle();
- $this->model->delete();
- // display success message
- $this->messageManager->addSuccess(__('The post has been deleted.'));
- // go to grid
- $this->_eventManager->dispatch(
- 'adminhtml_helloworld_on_delete',
- ['title' => $title, 'status' => 'success']
- );
- return $resultRedirect->setPath('*/*/');
- } catch (Exception $e) {
- $this->_eventManager->dispatch(
- 'adminhtml_helloworld_on_delete',
- ['title' => $title, 'status' => 'fail']
- );
- // display error message
- $this->messageManager->addError($e->getMessage());
- // go back to edit form
- return $resultRedirect->setPath('*/*/edit', ['id' => $id]);
- }
- }
- // display error message
- $this->messageManager->addError(__('We can't find a post to delete.'));
- // go to grid
- return $resultRedirect->setPath('*/*/');
- }
- }
Add Comment
Please, Sign In to add comment