Guest User

Untitled

a guest
Dec 11th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. <?php
  2.  
  3. namespace MageplazaHelloWorldControllerAdminhtmlPost;
  4.  
  5. use MagentoBackendAppAction;
  6.  
  7. class Delete extends MagentoBackendAppAction
  8. {
  9.  
  10. protected $model;
  11. public function __construct(
  12. ActionContext $context,
  13. MageplazaHelloWorldModelDataProvider $model
  14. ) {
  15. $this->model = $model;
  16. parent::__construct($context);
  17. }
  18. public function execute()
  19. {
  20. // check if we know what should be deleted
  21. $id = $this->getRequest()->getParam('id');
  22. /** @var MagentoBackendModelViewResultRedirect $resultRedirect */
  23. $resultRedirect = $this->resultRedirectFactory->create();
  24. if ($id) {
  25. $title = "";
  26. try {
  27. $this->model->load($id);
  28. $title = $this->model->getTitle();
  29. $this->model->delete();
  30. // display success message
  31. $this->messageManager->addSuccess(__('The post has been deleted.'));
  32. // go to grid
  33. $this->_eventManager->dispatch(
  34. 'adminhtml_helloworld_on_delete',
  35. ['title' => $title, 'status' => 'success']
  36. );
  37. return $resultRedirect->setPath('*/*/');
  38. } catch (Exception $e) {
  39. $this->_eventManager->dispatch(
  40. 'adminhtml_helloworld_on_delete',
  41. ['title' => $title, 'status' => 'fail']
  42. );
  43. // display error message
  44. $this->messageManager->addError($e->getMessage());
  45. // go back to edit form
  46. return $resultRedirect->setPath('*/*/edit', ['id' => $id]);
  47. }
  48. }
  49. // display error message
  50. $this->messageManager->addError(__('We can't find a post to delete.'));
  51. // go to grid
  52. return $resultRedirect->setPath('*/*/');
  53. }
  54. }
Add Comment
Please, Sign In to add comment