/
Edit.php
94 lines (84 loc) · 3.71 KB
/
Edit.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Catalog\Controller\Adminhtml\Product;
class Edit extends \Magento\Catalog\Controller\Adminhtml\Product
{
/**
* Array of actions which can be processed without secret key validation
*
* @var array
*/
protected $_publicActions = ['edit'];
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;
/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Catalog\Controller\Adminhtml\Product\Builder $productBuilder
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Catalog\Controller\Adminhtml\Product\Builder $productBuilder,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
parent::__construct($context, $productBuilder);
$this->resultPageFactory = $resultPageFactory;
}
/**
* Product edit form
*
* @return \Magento\Framework\Controller\ResultInterface
*/
public function execute()
{
/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
$storeManager = $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
$storeId = (int) $this->getRequest()->getParam('store', 0);
$store = $storeManager->getStore($storeId);
$storeManager->setCurrentStore($store->getCode());
$productId = (int) $this->getRequest()->getParam('id');
$product = $this->productBuilder->build($this->getRequest());
if (($productId && !$product->getEntityId())) {
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$this->messageManager->addErrorMessage(__('This product doesn\'t exist.'));
return $resultRedirect->setPath('catalog/*/');
} elseif ($productId === 0) {
/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$this->messageManager->addErrorMessage(__('Invalid product id. Should be numeric value greater than 0'));
return $resultRedirect->setPath('catalog/*/');
}
$this->_eventManager->dispatch('catalog_product_edit_action', ['product' => $product]);
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
$resultPage = $this->resultPageFactory->create();
$resultPage->addHandle('catalog_product_' . $product->getTypeId());
$resultPage->setActiveMenu('Magento_Catalog::catalog_products');
$resultPage->getConfig()->getTitle()->prepend(__('Products'));
$resultPage->getConfig()->getTitle()->prepend($product->getName());
if (!$this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)->isSingleStoreMode()
&&
($switchBlock = $resultPage->getLayout()->getBlock('store_switcher'))
) {
$switchBlock->setDefaultStoreName(__('Default Values'))
->setWebsiteIds($product->getWebsiteIds())
->setSwitchUrl(
$this->getUrl(
'catalog/*/*',
['_current' => true, 'active_tab' => null, 'tab' => null, 'store' => null]
)
);
}
$block = $resultPage->getLayout()->getBlock('catalog.wysiwyg.js');
if ($block) {
$block->setStoreId($product->getStoreId());
}
return $resultPage;
}
}