MassDelete.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. use Magento\Framework\Controller\ResultFactory;
  10. use Magento\Backend\App\Action\Context;
  11. use Magento\Ui\Component\MassAction\Filter;
  12. use Magento\Cms\Model\ResourceModel\Block\CollectionFactory;
  13. /**
  14. * Class MassDelete
  15. */
  16. class MassDelete extends \Magento\Backend\App\Action implements HttpPostActionInterface
  17. {
  18. /**
  19. * Authorization level of a basic admin session
  20. *
  21. * @see _isAllowed()
  22. */
  23. const ADMIN_RESOURCE = 'Magento_Cms::block';
  24. /**
  25. * @var Filter
  26. */
  27. protected $filter;
  28. /**
  29. * @var CollectionFactory
  30. */
  31. protected $collectionFactory;
  32. /**
  33. * @param Context $context
  34. * @param Filter $filter
  35. * @param CollectionFactory $collectionFactory
  36. */
  37. public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
  38. {
  39. $this->filter = $filter;
  40. $this->collectionFactory = $collectionFactory;
  41. parent::__construct($context);
  42. }
  43. /**
  44. * Execute action
  45. *
  46. * @return \Magento\Backend\Model\View\Result\Redirect
  47. * @throws \Magento\Framework\Exception\LocalizedException|\Exception
  48. */
  49. public function execute()
  50. {
  51. $collection = $this->filter->getCollection($this->collectionFactory->create());
  52. $collectionSize = $collection->getSize();
  53. foreach ($collection as $block) {
  54. $block->delete();
  55. }
  56. $this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been deleted.', $collectionSize));
  57. /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
  58. $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
  59. return $resultRedirect->setPath('*/*/');
  60. }
  61. }