Delete.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Cms\Controller\Adminhtml\Block;
  8. use Magento\Framework\App\Action\HttpPostActionInterface;
  9. class Delete extends \Magento\Cms\Controller\Adminhtml\Block implements HttpPostActionInterface
  10. {
  11. /**
  12. * Delete action
  13. *
  14. * @return \Magento\Framework\Controller\ResultInterface
  15. */
  16. public function execute()
  17. {
  18. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  19. $resultRedirect = $this->resultRedirectFactory->create();
  20. // check if we know what should be deleted
  21. $id = $this->getRequest()->getParam('block_id');
  22. if ($id) {
  23. try {
  24. // init model and delete
  25. $model = $this->_objectManager->create(\Magento\Cms\Model\Block::class);
  26. $model->load($id);
  27. $model->delete();
  28. // display success message
  29. $this->messageManager->addSuccessMessage(__('You deleted the block.'));
  30. // go to grid
  31. return $resultRedirect->setPath('*/*/');
  32. } catch (\Exception $e) {
  33. // display error message
  34. $this->messageManager->addErrorMessage($e->getMessage());
  35. // go back to edit form
  36. return $resultRedirect->setPath('*/*/edit', ['block_id' => $id]);
  37. }
  38. }
  39. // display error message
  40. $this->messageManager->addErrorMessage(__('We can\'t find a block to delete.'));
  41. // go to grid
  42. return $resultRedirect->setPath('*/*/');
  43. }
  44. }