filter = $filter; $this->collectionFactory = $collectionFactory; $this->synGroupRepository = $synGroupRepository; parent::__construct($context); } /** * 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(); $deletedItems = 0; foreach ($collection as $synonymGroup) { try { $this->synGroupRepository->delete($synonymGroup); $deletedItems++; } catch (\Exception $e) { $this->messageManager->addErrorMessage($e->getMessage()); } } if ($deletedItems != 0) { if ($collectionSize != $deletedItems) { $this->messageManager->addErrorMessage( __('Failed to delete %1 synonym group(s).', $collectionSize - $deletedItems) ); } $this->messageManager->addSuccessMessage( __('A total of %1 synonym group(s) have been deleted.', $deletedItems) ); } /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT); return $resultRedirect->setPath('*/*/'); } }