Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Edit.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
10 
12 {
18  protected $_publicActions = ['edit'];
19 
23  protected $resultPageFactory;
24 
30  public function __construct(
31  \Magento\Backend\App\Action\Context $context,
32  \Magento\Catalog\Controller\Adminhtml\Product\Builder $productBuilder,
33  \Magento\Framework\View\Result\PageFactory $resultPageFactory
34  ) {
35  parent::__construct($context, $productBuilder);
36  $this->resultPageFactory = $resultPageFactory;
37  }
38 
44  public function execute()
45  {
47  $storeManager = $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
48  $storeId = (int) $this->getRequest()->getParam('store', 0);
49  $store = $storeManager->getStore($storeId);
50  $storeManager->setCurrentStore($store->getCode());
51  $productId = (int) $this->getRequest()->getParam('id');
52  $product = $this->productBuilder->build($this->getRequest());
53 
54  if (($productId && !$product->getEntityId())) {
56  $resultRedirect = $this->resultRedirectFactory->create();
57  $this->messageManager->addErrorMessage(__('This product doesn\'t exist.'));
58  return $resultRedirect->setPath('catalog/*/');
59  } elseif ($productId === 0) {
61  $resultRedirect = $this->resultRedirectFactory->create();
62  $this->messageManager->addErrorMessage(__('Invalid product id. Should be numeric value greater than 0'));
63  return $resultRedirect->setPath('catalog/*/');
64  }
65 
66  $this->_eventManager->dispatch('catalog_product_edit_action', ['product' => $product]);
67 
69  $resultPage = $this->resultPageFactory->create();
70  $resultPage->addHandle('catalog_product_' . $product->getTypeId());
71  $resultPage->setActiveMenu('Magento_Catalog::catalog_products');
72  $resultPage->getConfig()->getTitle()->prepend(__('Products'));
73  $resultPage->getConfig()->getTitle()->prepend($product->getName());
74 
75  if (!$this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)->isSingleStoreMode()
76  &&
77  ($switchBlock = $resultPage->getLayout()->getBlock('store_switcher'))
78  ) {
79  $switchBlock->setDefaultStoreName(__('Default Values'))
80  ->setWebsiteIds($product->getWebsiteIds())
81  ->setSwitchUrl(
82  $this->getUrl(
83  'catalog/*/*',
84  ['_current' => true, 'active_tab' => null, 'tab' => null, 'store' => null]
85  )
86  );
87  }
88 
89  return $resultPage;
90  }
91 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$storeManager
__()
Definition: __.php:13
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Catalog\Controller\Adminhtml\Product\Builder $productBuilder, \Magento\Framework\View\Result\PageFactory $resultPageFactory)
Definition: Edit.php:30