filter = $filter; $this->collectionFactory = $collectionFactory; parent::__construct($context); } /** * @return Redirect|ResponseInterface|ResultInterface * @throws LocalizedException */ public function execute() { $collection = $this->filter->getCollection($this->collectionFactory->create()); $authorRemove = 0; foreach ($collection->getItems() as $author) { if ($author->hasPost()) { $this->messageManager->addWarningMessage(__('One of the authors has post. You can not delete this one.')); } else { try { $author->delete(); $authorRemove++; } catch (Exception $e) { $this->_getSession()->addException( $e, __('Something went wrong while updating status for %1.', $author->getName()) ); } } } if ($authorRemove) { $this->messageManager->addSuccessMessage(__('A total of %1 record(s) have been removed.', $authorRemove)); } /** @var Redirect $resultRedirect */ $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT); return $resultRedirect->setPath('*/*/'); } }