/
Delete.php
56 lines (52 loc) · 2.22 KB
/
Delete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Controller\Adminhtml\Category;
class Delete extends \Magento\Catalog\Controller\Adminhtml\Category
{
/** @var \Magento\Catalog\Api\CategoryRepositoryInterface */
protected $categoryRepository;
/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository
) {
parent::__construct($context);
$this->categoryRepository = $categoryRepository;
}
/**
* Delete category action
*
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$categoryId = (int)$this->getRequest()->getParam('id');
$parentId = null;
if ($categoryId) {
try {
$category = $this->categoryRepository->get($categoryId);
$parentId = $category->getParentId();
$this->_eventManager->dispatch('catalog_controller_category_delete', ['category' => $category]);
$this->_auth->getAuthStorage()->setDeletedPath($category->getPath());
$this->categoryRepository->delete($category);
$this->messageManager->addSuccess(__('You deleted the category.'));
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
return $resultRedirect->setPath('catalog/*/edit', ['_current' => true]);
} catch (\Exception $e) {
$this->messageManager->addError(__('Something went wrong while trying to delete the category.'));
return $resultRedirect->setPath('catalog/*/edit', ['_current' => true]);
}
}
return $resultRedirect->setPath('catalog/*/', ['_current' => true, 'id' => $parentId]);
}
}