Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MassDelete.php
Go to the documentation of this file.
1 <?php
8 
15 {
21  const ADMIN_RESOURCE = 'Magento_Search::synonyms';
22 
26  private $filter;
27 
31  private $collectionFactory;
32 
36  private $synGroupRepository;
37 
46  public function __construct(
47  \Magento\Backend\App\Action\Context $context,
48  \Magento\Ui\Component\MassAction\Filter $filter,
49  \Magento\Search\Model\ResourceModel\SynonymGroup\CollectionFactory $collectionFactory,
50  \Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository
51  ) {
52  $this->filter = $filter;
53  $this->collectionFactory = $collectionFactory;
54  $this->synGroupRepository = $synGroupRepository;
55  parent::__construct($context);
56  }
57 
64  public function execute()
65  {
66  $collection = $this->filter->getCollection($this->collectionFactory->create());
67  $collectionSize = $collection->getSize();
68 
69  $deletedItems = 0;
70  foreach ($collection as $synonymGroup) {
71  try {
72  $this->synGroupRepository->delete($synonymGroup);
73  $deletedItems++;
74  } catch (\Exception $e) {
75  $this->messageManager->addErrorMessage($e->getMessage());
76  }
77  }
78  if ($deletedItems != 0) {
79  if ($collectionSize != $deletedItems) {
80  $this->messageManager->addErrorMessage(
81  __('Failed to delete %1 synonym group(s).', $collectionSize - $deletedItems)
82  );
83  }
84 
85  $this->messageManager->addSuccessMessage(
86  __('A total of %1 synonym group(s) have been deleted.', $deletedItems)
87  );
88  }
90  $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
91  return $resultRedirect->setPath('*/*/');
92  }
93 }
__()
Definition: __.php:13
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Ui\Component\MassAction\Filter $filter, \Magento\Search\Model\ResourceModel\SynonymGroup\CollectionFactory $collectionFactory, \Magento\Search\Api\SynonymGroupRepositoryInterface $synGroupRepository)
Definition: MassDelete.php:46