Skip to content

Commit 38dfc6e

Browse files
author
Alexander Akimov
authored
Merge pull request #2805 from magento-tsg/2.2-develop-pr33
[TSG] Backporting for 2.2 (pr33) (2.2.6)
2 parents 03d5c35 + 642ac55 commit 38dfc6e

34 files changed

+1035
-139
lines changed

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

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,27 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Catalog\Controller\Adminhtml;
79

10+
use Magento\Store\Model\Store;
11+
use Magento\Backend\App\Action\Context;
12+
use Magento\Framework\Stdlib\DateTime\Filter\Date;
13+
use Magento\Catalog\Model\Category as CategoryModel;
14+
use Magento\Backend\App\Action;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Framework\Registry;
17+
use Magento\Cms\Model\Wysiwyg\Config;
18+
use Magento\Backend\Model\View\Result\Page;
19+
use Magento\Framework\Controller\Result\Json;
20+
use Magento\Backend\Model\Auth\Session;
21+
use Magento\Framework\DataObject;
22+
823
/**
924
* Catalog category controller
1025
*/
11-
abstract class Category extends \Magento\Backend\App\Action
26+
abstract class Category extends Action
1227
{
1328
/**
1429
* Authorization level of a basic admin session
@@ -18,18 +33,16 @@ abstract class Category extends \Magento\Backend\App\Action
1833
const ADMIN_RESOURCE = 'Magento_Catalog::categories';
1934

2035
/**
21-
* @var \Magento\Framework\Stdlib\DateTime\Filter\Date
36+
* @var Date
2237
*/
2338
protected $dateFilter;
2439

2540
/**
26-
* @param \Magento\Backend\App\Action\Context $context
27-
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date|null $dateFilter
41+
* @param Context $context
42+
* @param Date|null $dateFilter
2843
*/
29-
public function __construct(
30-
\Magento\Backend\App\Action\Context $context,
31-
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter = null
32-
) {
44+
public function __construct(Context $context, Date $dateFilter = null)
45+
{
3346
$this->dateFilter = $dateFilter;
3447
parent::__construct($context);
3548
}
@@ -39,20 +52,20 @@ public function __construct(
3952
* Root category can be returned, if inappropriate store/category is specified
4053
*
4154
* @param bool $getRootInstead
42-
* @return \Magento\Catalog\Model\Category|false
55+
* @return CategoryModel|false
4356
*/
44-
protected function _initCategory($getRootInstead = false)
57+
protected function _initCategory(bool $getRootInstead = false)
4558
{
4659
$categoryId = $this->resolveCategoryId();
47-
$storeId = (int)$this->getRequest()->getParam('store');
48-
$category = $this->_objectManager->create(\Magento\Catalog\Model\Category::class);
60+
$storeId = $this->resolveStoreId();
61+
$category = $this->_objectManager->create(CategoryModel::class);
4962
$category->setStoreId($storeId);
5063

5164
if ($categoryId) {
5265
$category->load($categoryId);
5366
if ($storeId) {
5467
$rootId = $this->_objectManager->get(
55-
\Magento\Store\Model\StoreManagerInterface::class
68+
StoreManagerInterface::class
5669
)->getStore(
5770
$storeId
5871
)->getRootCategoryId();
@@ -67,9 +80,9 @@ protected function _initCategory($getRootInstead = false)
6780
}
6881
}
6982

70-
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('category', $category);
71-
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
72-
$this->_objectManager->get(\Magento\Cms\Model\Wysiwyg\Config::class)
83+
$this->_objectManager->get(Registry::class)->register('category', $category);
84+
$this->_objectManager->get(Registry::class)->register('current_category', $category);
85+
$this->_objectManager->get(Config::class)
7386
->setStoreId($this->getRequest()->getParam('store'));
7487
return $category;
7588
}
@@ -79,31 +92,46 @@ protected function _initCategory($getRootInstead = false)
7992
*
8093
* @return int
8194
*/
82-
private function resolveCategoryId()
95+
private function resolveCategoryId(): int
8396
{
8497
$categoryId = (int)$this->getRequest()->getParam('id', false);
8598

8699
return $categoryId ?: (int)$this->getRequest()->getParam('entity_id', false);
87100
}
88101

102+
/**
103+
* Resolve store id
104+
*
105+
* Tries to take store id from store HTTP parameter
106+
* @see Store
107+
*
108+
* @return int
109+
*/
110+
private function resolveStoreId(): int
111+
{
112+
$storeId = (int)$this->getRequest()->getParam('store', false);
113+
114+
return $storeId ?: (int)$this->getRequest()->getParam('store_id', Store::DEFAULT_STORE_ID);
115+
}
116+
89117
/**
90118
* Build response for ajax request
91119
*
92-
* @param \Magento\Catalog\Model\Category $category
93-
* @param \Magento\Backend\Model\View\Result\Page $resultPage
120+
* @param CategoryModel $category
121+
* @param Page $resultPage
94122
*
95-
* @return \Magento\Framework\Controller\Result\Json
123+
* @return Json
96124
*
97125
* @deprecated 101.0.0
98126
*/
99-
protected function ajaxRequestResponse($category, $resultPage)
127+
protected function ajaxRequestResponse(CategoryModel $category, Page $resultPage): Json
100128
{
101129
// prepare breadcrumbs of selected category, if any
102130
$breadcrumbsPath = $category->getPath();
103131
if (empty($breadcrumbsPath)) {
104132
// but if no category, and it is deleted - prepare breadcrumbs from path, saved in session
105133
$breadcrumbsPath = $this->_objectManager->get(
106-
\Magento\Backend\Model\Auth\Session::class
134+
Session::class
107135
)->getDeletedPath(
108136
true
109137
);
@@ -119,7 +147,7 @@ protected function ajaxRequestResponse($category, $resultPage)
119147
}
120148
}
121149

122-
$eventResponse = new \Magento\Framework\DataObject([
150+
$eventResponse = new DataObject([
123151
'content' => $resultPage->getLayout()->getUiComponent('category_form')->getFormHtml()
124152
. $resultPage->getLayout()->getBlock('category.tree')
125153
->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'),
@@ -130,22 +158,23 @@ protected function ajaxRequestResponse($category, $resultPage)
130158
'category_prepare_ajax_response',
131159
['response' => $eventResponse, 'controller' => $this]
132160
);
133-
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
134-
$resultJson = $this->_objectManager->get(\Magento\Framework\Controller\Result\Json::class);
161+
/** @var Json $resultJson */
162+
$resultJson = $this->_objectManager->get(Json::class);
135163
$resultJson->setHeader('Content-type', 'application/json', true);
136164
$resultJson->setData($eventResponse->getData());
165+
137166
return $resultJson;
138167
}
139168

140169
/**
141170
* Datetime data preprocessing
142171
*
143-
* @param \Magento\Catalog\Model\Category $category
172+
* @param CategoryModel $category
144173
* @param array $postData
145174
*
146175
* @return array
147176
*/
148-
protected function dateTimePreprocessing($category, $postData)
177+
protected function dateTimePreprocessing(CategoryModel $category, array $postData): array
149178
{
150179
$dateFieldFilters = [];
151180
$attributes = $category->getAttributes();

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -322,12 +322,10 @@ protected function initializeProductData(array $productData, $createNew)
322322
unset($productData['media_gallery']);
323323
if ($createNew) {
324324
$product = $this->productFactory->create();
325+
$this->assignProductToWebsites($product);
325326
if (isset($productData['price']) && !isset($productData['product_type'])) {
326327
$product->setTypeId(Product\Type::TYPE_SIMPLE);
327328
}
328-
if ($this->storeManager->hasSingleStore()) {
329-
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsiteId()]);
330-
}
331329
} else {
332330
if (!empty($productData['id'])) {
333331
unset($this->instancesById[$productData['id']]);
@@ -341,31 +339,20 @@ protected function initializeProductData(array $productData, $createNew)
341339
foreach ($productData as $key => $value) {
342340
$product->setData($key, $value);
343341
}
344-
$this->assignProductToWebsites($product, $createNew);
345342

346343
return $product;
347344
}
348345

349346
/**
350347
* @param \Magento\Catalog\Model\Product $product
351-
* @param bool $createNew
352348
* @return void
353349
*/
354-
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product, $createNew)
350+
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product)
355351
{
356-
$websiteIds = $product->getWebsiteIds();
357-
358-
if (!$this->storeManager->hasSingleStore()) {
359-
$websiteIds = array_unique(
360-
array_merge(
361-
$websiteIds,
362-
[$this->storeManager->getStore()->getWebsiteId()]
363-
)
364-
);
365-
}
366-
367-
if ($createNew && $this->storeManager->getStore(true)->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) {
352+
if ($this->storeManager->getStore(true)->getCode() === \Magento\Store\Model\Store::ADMIN_CODE) {
368353
$websiteIds = array_keys($this->storeManager->getWebsites());
354+
} else {
355+
$websiteIds = [$this->storeManager->getStore()->getWebsiteId()];
369356
}
370357

371358
$product->setWebsiteIds($websiteIds);

0 commit comments

Comments
 (0)