Skip to content

Commit 6fde4ba

Browse files
committed
revert CategoryRepository, added plugin
1 parent df69e82 commit 6fde4ba

File tree

3 files changed

+71
-23
lines changed

3 files changed

+71
-23
lines changed

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
namespace Magento\Catalog\Model;
99

10-
use Magento\Catalog\Api\Data\CategoryInterface;
1110
use Magento\Framework\Exception\CouldNotSaveException;
1211
use Magento\Framework\Exception\NoSuchEntityException;
1312
use Magento\Framework\Exception\StateException;
13+
use Magento\Catalog\Api\Data\CategoryInterface;
1414

1515
/**
1616
* Repository for categories.
@@ -77,7 +77,10 @@ public function __construct(
7777
public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
7878
{
7979
$storeId = (int)$this->storeManager->getStore()->getId();
80-
$existingData = $this->getExistingData($category, $storeId);
80+
$existingData = $this->getExtensibleDataObjectConverter()
81+
->toNestedArray($category, [], \Magento\Catalog\Api\Data\CategoryInterface::class);
82+
$existingData = array_diff_key($existingData, array_flip(['path', 'level', 'parent_id']));
83+
$existingData['store_id'] = $storeId;
8184

8285
if ($category->getId()) {
8386
$metadata = $this->getMetadataPool()->getMetadata(
@@ -236,25 +239,4 @@ private function getMetadataPool()
236239
}
237240
return $this->metadataPool;
238241
}
239-
240-
/**
241-
* Get existing data category
242-
*
243-
* @param CategoryInterface $category
244-
* @param int $storeId
245-
* @return array
246-
*/
247-
private function getExistingData(CategoryInterface $category, int $storeId)
248-
{
249-
$existingData = $this->getExtensibleDataObjectConverter()
250-
->toNestedArray($category, [], \Magento\Catalog\Api\Data\CategoryInterface::class);
251-
$existingData = array_diff_key($existingData, array_flip(['path', 'level', 'parent_id']));
252-
$existingData['store_id'] = $storeId;
253-
254-
if ($category->getData('save_rewrites_history') !== null) {
255-
$existingData['save_rewrites_history'] = $category->getData('save_rewrites_history');
256-
}
257-
258-
return $existingData;
259-
}
260242
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogUrlRewrite\Plugin\Model;
9+
10+
use Magento\Catalog\Model\Category;
11+
use Magento\Framework\Webapi\Rest\Request as RestRequest;
12+
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
13+
14+
class UpdateCategoryDataList
15+
{
16+
private const SAVE_REWRITES_HISTORY = 'save_rewrites_history';
17+
18+
/**
19+
* @var RestRequest
20+
*/
21+
private $request;
22+
23+
/**
24+
* @param RestRequest $request
25+
*/
26+
public function __construct(RestRequest $request)
27+
{
28+
$this->request = $request;
29+
}
30+
31+
/**
32+
* Add 'save_rewrites_history' param to the category for list
33+
*
34+
* @param CategoryUrlRewriteGenerator $subject
35+
* @param Category $category
36+
* @return void
37+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
38+
*/
39+
public function beforeGenerate(CategoryUrlRewriteGenerator $subject, Category $category)
40+
{
41+
$requestBodyParams = $this->request->getBodyParams();
42+
43+
if ($this->isCustomAttributesExists($requestBodyParams, CategoryUrlRewriteGenerator::ENTITY_TYPE)) {
44+
foreach ($requestBodyParams[CategoryUrlRewriteGenerator::ENTITY_TYPE]['custom_attributes'] as $attribute) {
45+
if ($attribute['attribute_code'] === self::SAVE_REWRITES_HISTORY) {
46+
$category->setData(self::SAVE_REWRITES_HISTORY, (bool)$attribute['value']);
47+
}
48+
}
49+
}
50+
}
51+
52+
/**
53+
* Check is any custom options exists in data
54+
*
55+
* @param array $requestBodyParams
56+
* @param string $entityCode
57+
* @return bool
58+
*/
59+
private function isCustomAttributesExists(array $requestBodyParams, string $entityCode): bool
60+
{
61+
return !empty($requestBodyParams[$entityCode]['custom_attributes']);
62+
}
63+
}

app/code/Magento/CatalogUrlRewrite/etc/webapi_rest/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
<plugin name="product_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\ProductInputParamsResolver" disabled="false" />
1111
<plugin name="category_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Webapi\Controller\Rest\CategoryInputParamsResolver" disabled="false" />
1212
</type>
13+
<type name="Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator">
14+
<plugin name="category_set_save_rewrites_history_rest_plugin" type="Magento\CatalogUrlRewrite\Plugin\Model\UpdateCategoryDataList" disabled="false" />
15+
</type>
1316
</config>

0 commit comments

Comments
 (0)