Skip to content

Commit c7f071f

Browse files
Merge pull request #4910 from magento-engcom/2.3-develop-fast-lane-prs
[Magento Community Engineering] Community Contributions - 2.3-develop latest changes
2 parents 0583fc3 + 07bcf5c commit c7f071f

File tree

76 files changed

+1786
-862
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1786
-862
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category.php

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77

88
namespace Magento\Catalog\Controller\Adminhtml;
99

10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Store\Model\Store;
12+
use Magento\Framework\Controller\ResultFactory;
1113

1214
/**
1315
* Catalog category controller
16+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1417
*/
1518
abstract class Category extends \Magento\Backend\App\Action
1619
{
@@ -26,20 +29,61 @@ abstract class Category extends \Magento\Backend\App\Action
2629
*/
2730
protected $dateFilter;
2831

32+
/**
33+
* @var \Magento\Store\Model\StoreManagerInterface
34+
*/
35+
private $storeManager;
36+
37+
/**
38+
* @var \Magento\Framework\Registry
39+
*/
40+
private $registry;
41+
42+
/**
43+
* @var \Magento\Cms\Model\Wysiwyg\Config
44+
*/
45+
private $wysiwigConfig;
46+
47+
/**
48+
* @var \Magento\Backend\Model\Auth\Session
49+
*/
50+
private $authSession;
51+
2952
/**
3053
* @param \Magento\Backend\App\Action\Context $context
3154
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date|null $dateFilter
55+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
56+
* @param \Magento\Framework\Registry $registry
57+
* @param \Magento\Cms\Model\Wysiwyg\Config $wysiwigConfig
58+
* @param \Magento\Backend\Model\Auth\Session $authSession
3259
*/
3360
public function __construct(
3461
\Magento\Backend\App\Action\Context $context,
35-
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter = null
62+
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter = null,
63+
\Magento\Store\Model\StoreManagerInterface $storeManager = null,
64+
\Magento\Framework\Registry $registry = null,
65+
\Magento\Cms\Model\Wysiwyg\Config $wysiwigConfig = null,
66+
\Magento\Backend\Model\Auth\Session $authSession = null
3667
) {
3768
$this->dateFilter = $dateFilter;
69+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(
70+
\Magento\Store\Model\StoreManagerInterface::class
71+
);
72+
$this->registry = $registry ?: ObjectManager::getInstance()->get(
73+
\Magento\Framework\Registry::class
74+
);
75+
$this->wysiwigConfig = $wysiwigConfig ?: ObjectManager::getInstance()->get(
76+
\Magento\Cms\Model\Wysiwyg\Config::class
77+
);
78+
$this->authSession = $authSession ?: ObjectManager::getInstance()->get(
79+
\Magento\Backend\Model\Auth\Session::class
80+
);
3881
parent::__construct($context);
3982
}
4083

4184
/**
42-
* Initialize requested category and put it into registry.
85+
* Initialize requested category and put it into registry
86+
*
4387
* Root category can be returned, if inappropriate store/category is specified
4488
*
4589
* @param bool $getRootInstead
@@ -55,11 +99,7 @@ protected function _initCategory($getRootInstead = false)
5599
if ($categoryId) {
56100
$category->load($categoryId);
57101
if ($storeId) {
58-
$rootId = $this->_objectManager->get(
59-
\Magento\Store\Model\StoreManagerInterface::class
60-
)->getStore(
61-
$storeId
62-
)->getRootCategoryId();
102+
$rootId = $this->storeManager->getStore($storeId)->getRootCategoryId();
63103
if (!in_array($rootId, $category->getPathIds())) {
64104
// load root category instead wrong one
65105
if ($getRootInstead) {
@@ -71,10 +111,9 @@ protected function _initCategory($getRootInstead = false)
71111
}
72112
}
73113

74-
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('category', $category);
75-
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
76-
$this->_objectManager->get(\Magento\Cms\Model\Wysiwyg\Config::class)
77-
->setStoreId($storeId);
114+
$this->registry->register('category', $category);
115+
$this->registry->register('current_category', $category);
116+
$this->wysiwigConfig->setStoreId($storeId);
78117
return $category;
79118
}
80119

@@ -91,9 +130,8 @@ private function resolveCategoryId() : int
91130
}
92131

93132
/**
94-
* Resolve store id
133+
* Resolve store Id, tries to take store id from store HTTP parameter
95134
*
96-
* Tries to take store id from store HTTP parameter
97135
* @see Store
98136
*
99137
* @return int
@@ -121,11 +159,7 @@ protected function ajaxRequestResponse($category, $resultPage)
121159
$breadcrumbsPath = $category->getPath();
122160
if (empty($breadcrumbsPath)) {
123161
// but if no category, and it is deleted - prepare breadcrumbs from path, saved in session
124-
$breadcrumbsPath = $this->_objectManager->get(
125-
\Magento\Backend\Model\Auth\Session::class
126-
)->getDeletedPath(
127-
true
128-
);
162+
$breadcrumbsPath = $this->authSession->getDeletedPath(true);
129163
if (!empty($breadcrumbsPath)) {
130164
$breadcrumbsPath = explode('/', $breadcrumbsPath);
131165
// no need to get parent breadcrumbs if deleting category level 1
@@ -138,19 +172,21 @@ protected function ajaxRequestResponse($category, $resultPage)
138172
}
139173
}
140174

141-
$eventResponse = new \Magento\Framework\DataObject([
142-
'content' => $resultPage->getLayout()->getUiComponent('category_form')->getFormHtml()
143-
. $resultPage->getLayout()->getBlock('category.tree')
144-
->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'),
145-
'messages' => $resultPage->getLayout()->getMessagesBlock()->getGroupedHtml(),
146-
'toolbar' => $resultPage->getLayout()->getBlock('page.actions.toolbar')->toHtml()
147-
]);
175+
$eventResponse = new \Magento\Framework\DataObject(
176+
[
177+
'content' => $resultPage->getLayout()->getUiComponent('category_form')->getFormHtml()
178+
. $resultPage->getLayout()->getBlock('category.tree')
179+
->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'),
180+
'messages' => $resultPage->getLayout()->getMessagesBlock()->getGroupedHtml(),
181+
'toolbar' => $resultPage->getLayout()->getBlock('page.actions.toolbar')->toHtml()
182+
]
183+
);
148184
$this->_eventManager->dispatch(
149185
'category_prepare_ajax_response',
150186
['response' => $eventResponse, 'controller' => $this]
151187
);
152188
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
153-
$resultJson = $this->_objectManager->get(\Magento\Framework\Controller\Result\Json::class);
189+
$resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
154190
$resultJson->setHeader('Content-type', 'application/json', true);
155191
$resultJson->setData($eventResponse->getData());
156192
return $resultJson;

app/code/Magento/Catalog/Controller/Adminhtml/Category/CategoriesJson.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Catalog\Controller\Adminhtml\Category;
87

98
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
9+
use Magento\Framework\App\ObjectManager;
1010

11+
/**
12+
* Class CategoriesJson
13+
*/
1114
class CategoriesJson extends \Magento\Catalog\Controller\Adminhtml\Category implements HttpPostActionInterface
1215
{
1316
/**
@@ -20,19 +23,28 @@ class CategoriesJson extends \Magento\Catalog\Controller\Adminhtml\Category impl
2023
*/
2124
protected $layoutFactory;
2225

26+
/**
27+
* @var \Magento\Backend\Model\Auth\Session
28+
*/
29+
private $authSession;
30+
2331
/**
2432
* @param \Magento\Backend\App\Action\Context $context
2533
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
2634
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
35+
* @param \Magento\Backend\Model\Auth\Session $authSession
2736
*/
2837
public function __construct(
2938
\Magento\Backend\App\Action\Context $context,
3039
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
31-
\Magento\Framework\View\LayoutFactory $layoutFactory
40+
\Magento\Framework\View\LayoutFactory $layoutFactory,
41+
\Magento\Backend\Model\Auth\Session $authSession = null
3242
) {
3343
parent::__construct($context);
3444
$this->resultJsonFactory = $resultJsonFactory;
3545
$this->layoutFactory = $layoutFactory;
46+
$this->authSession = $authSession ?: ObjectManager::getInstance()
47+
->get(\Magento\Backend\Model\Auth\Session::class);
3648
}
3749

3850
/**
@@ -43,9 +55,9 @@ public function __construct(
4355
public function execute()
4456
{
4557
if ($this->getRequest()->getParam('expand_all')) {
46-
$this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class)->setIsTreeWasExpanded(true);
58+
$this->authSession->setIsTreeWasExpanded(true);
4759
} else {
48-
$this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class)->setIsTreeWasExpanded(false);
60+
$this->authSession->setIsTreeWasExpanded(false);
4961
}
5062
$categoryId = (int)$this->getRequest()->getPost('id');
5163
if ($categoryId) {

app/code/Magento/Catalog/Controller/Adminhtml/Category/Edit.php

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
<?php
22
/**
3-
*
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
76
namespace Magento\Catalog\Controller\Adminhtml\Category;
87

98
use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;
9+
use Magento\Framework\App\ObjectManager;
1010

11+
/**
12+
* Class Edit
13+
*/
1114
class Edit extends \Magento\Catalog\Controller\Adminhtml\Category implements HttpGetActionInterface
1215
{
1316
/**
@@ -27,18 +30,23 @@ class Edit extends \Magento\Catalog\Controller\Adminhtml\Category implements Htt
2730

2831
/**
2932
* Edit constructor.
33+
*
3034
* @param \Magento\Backend\App\Action\Context $context
3135
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
3236
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
37+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
3338
*/
3439
public function __construct(
3540
\Magento\Backend\App\Action\Context $context,
3641
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
37-
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
42+
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
43+
\Magento\Store\Model\StoreManagerInterface $storeManager = null
3844
) {
3945
parent::__construct($context);
4046
$this->resultPageFactory = $resultPageFactory;
4147
$this->resultJsonFactory = $resultJsonFactory;
48+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()
49+
->get(\Magento\Store\Model\StoreManagerInterface::class);
4250
}
4351

4452
/**
@@ -51,20 +59,20 @@ public function __construct(
5159
public function execute()
5260
{
5361
$storeId = (int)$this->getRequest()->getParam('store');
54-
$store = $this->getStoreManager()->getStore($storeId);
55-
$this->getStoreManager()->setCurrentStore($store->getCode());
62+
$store = $this->storeManager->getStore($storeId);
63+
$this->storeManager->setCurrentStore($store->getCode());
5664

5765
$categoryId = (int)$this->getRequest()->getParam('id');
5866

5967
if (!$categoryId) {
6068
if ($storeId) {
61-
$categoryId = (int)$this->getStoreManager()->getStore($storeId)->getRootCategoryId();
69+
$categoryId = (int)$this->storeManager->getStore($storeId)->getRootCategoryId();
6270
} else {
63-
$defaultStoreView = $this->getStoreManager()->getDefaultStoreView();
71+
$defaultStoreView = $this->storeManager->getDefaultStoreView();
6472
if ($defaultStoreView) {
6573
$categoryId = (int)$defaultStoreView->getRootCategoryId();
6674
} else {
67-
$stores = $this->getStoreManager()->getStores();
75+
$stores = $this->storeManager->getStores();
6876
if (count($stores)) {
6977
$store = reset($stores);
7078
$categoryId = (int)$store->getRootCategoryId();
@@ -109,16 +117,4 @@ public function execute()
109117

110118
return $resultPage;
111119
}
112-
113-
/**
114-
* @return \Magento\Store\Model\StoreManagerInterface
115-
*/
116-
private function getStoreManager()
117-
{
118-
if (null === $this->storeManager) {
119-
$this->storeManager = \Magento\Framework\App\ObjectManager::getInstance()
120-
->get(\Magento\Store\Model\StoreManagerInterface::class);
121-
}
122-
return $this->storeManager;
123-
}
124120
}

app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
1010
use Magento\Catalog\Api\Data\CategoryAttributeInterface;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Store\Model\StoreManagerInterface;
1213

1314
/**
@@ -56,6 +57,11 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category implements Htt
5657
*/
5758
private $eavConfig;
5859

60+
/**
61+
* @var \Psr\Log\LoggerInterface
62+
*/
63+
private $logger;
64+
5965
/**
6066
* Constructor
6167
*
@@ -66,6 +72,7 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category implements Htt
6672
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter
6773
* @param StoreManagerInterface $storeManager
6874
* @param \Magento\Eav\Model\Config $eavConfig
75+
* @param \Psr\Log\LoggerInterface $logger
6976
*/
7077
public function __construct(
7178
\Magento\Backend\App\Action\Context $context,
@@ -74,15 +81,18 @@ public function __construct(
7481
\Magento\Framework\View\LayoutFactory $layoutFactory,
7582
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter,
7683
StoreManagerInterface $storeManager,
77-
\Magento\Eav\Model\Config $eavConfig = null
84+
\Magento\Eav\Model\Config $eavConfig = null,
85+
\Psr\Log\LoggerInterface $logger = null
7886
) {
7987
parent::__construct($context, $dateFilter);
8088
$this->resultRawFactory = $resultRawFactory;
8189
$this->resultJsonFactory = $resultJsonFactory;
8290
$this->layoutFactory = $layoutFactory;
8391
$this->storeManager = $storeManager;
84-
$this->eavConfig = $eavConfig
85-
?: \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Eav\Model\Config::class);
92+
$this->eavConfig = $eavConfig ?: ObjectManager::getInstance()
93+
->get(\Magento\Eav\Model\Config::class);
94+
$this->logger = $logger ?: ObjectManager::getInstance()
95+
->get(\Psr\Log\LoggerInterface::class);
8696
}
8797

8898
/**
@@ -210,7 +220,9 @@ public function execute()
210220
__('The "%1" attribute is required. Enter and try again.', $attribute)
211221
);
212222
} else {
213-
throw new \Exception($error);
223+
$this->messageManager->addErrorMessage(__('Something went wrong while saving the category.'));
224+
$this->logger->critical('Something went wrong while saving the category.');
225+
$this->_getSession()->setCategoryData($categoryPostData);
214226
}
215227
}
216228
}
@@ -221,11 +233,7 @@ public function execute()
221233
$this->messageManager->addSuccessMessage(__('You saved the category.'));
222234
} catch (\Magento\Framework\Exception\LocalizedException $e) {
223235
$this->messageManager->addExceptionMessage($e);
224-
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
225-
$this->_getSession()->setCategoryData($categoryPostData);
226-
} catch (\Exception $e) {
227-
$this->messageManager->addErrorMessage(__('Something went wrong while saving the category.'));
228-
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
236+
$this->logger->critical($e);
229237
$this->_getSession()->setCategoryData($categoryPostData);
230238
}
231239
}
@@ -332,11 +340,7 @@ protected function getParentCategory($parentId, $storeId)
332340
{
333341
if (!$parentId) {
334342
if ($storeId) {
335-
$parentId = $this->_objectManager->get(
336-
\Magento\Store\Model\StoreManagerInterface::class
337-
)->getStore(
338-
$storeId
339-
)->getRootCategoryId();
343+
$parentId = $this->storeManager->getStore($storeId)->getRootCategoryId();
340344
} else {
341345
$parentId = \Magento\Catalog\Model\Category::TREE_ROOT_ID;
342346
}

0 commit comments

Comments
 (0)