Skip to content

Commit e50d9a4

Browse files
author
mastiuhin-olexandr
committed
MC-40031: [UX] Configurations are not preserved on form reload when new configurable product creation fails
1 parent 3ca6264 commit e50d9a4

File tree

2 files changed

+19
-37
lines changed

2 files changed

+19
-37
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Magento\Framework\App\ObjectManager;
1414
use Magento\Store\Model\StoreManagerInterface;
1515
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
16-
use Magento\Catalog\Model\Product as ProductModel;
1716
use Magento\CatalogUrlRewrite\Model\Product\Validator as ProductUrlRewriteValidator;
1817

1918
/**
@@ -141,7 +140,7 @@ public function execute()
141140
$resource->getAttribute('news_from_date')->setMaxValue($product->getNewsToDate());
142141
$resource->getAttribute('custom_design_from')->setMaxValue($product->getCustomDesignTo());
143142

144-
$this->validateUrlKeyUniqueness($product);
143+
$this->productUrlRewriteValidator->validateUrlKey($product);
145144
$this->productValidator->validate($product, $this->getRequest(), $response);
146145
} catch (\Magento\Eav\Model\Entity\Attribute\Exception $e) {
147146
$response->setError(true);
@@ -167,32 +166,6 @@ public function execute()
167166
return $this->resultJsonFactory->create()->setData($response);
168167
}
169168

170-
/**
171-
* Validates Url Key uniqueness.
172-
*
173-
* @param ProductModel $product
174-
* @throws UrlAlreadyExistsException
175-
*/
176-
private function validateUrlKeyUniqueness(ProductModel $product): void
177-
{
178-
$conflictingUrlRewrites = $this->productUrlRewriteValidator->findUrlKeyConflicts($product);
179-
180-
if ($conflictingUrlRewrites) {
181-
$data = [];
182-
183-
foreach ($conflictingUrlRewrites as $urlRewrite) {
184-
$data[$urlRewrite->getUrlRewriteId()] = $urlRewrite->toArray();
185-
}
186-
187-
throw new UrlAlreadyExistsException(
188-
__('URL key for specified store already exists.'),
189-
null,
190-
0,
191-
$data
192-
);
193-
}
194-
}
195-
196169
/**
197170
* @return StoreManagerInterface
198171
* @deprecated 101.0.0

app/code/Magento/CatalogUrlRewrite/Model/Product/Validator.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Catalog\Model\Product;
1111
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
12+
use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
1213
use Magento\UrlRewrite\Model\UrlFinderInterface;
1314
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
1415
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
@@ -50,12 +51,12 @@ public function __construct(
5051
}
5152

5253
/**
53-
* Find Url Key conflicts of a product.
54+
* Validate Url Key of a Product.
5455
*
5556
* @param Product $product
56-
* @return array Array of conflicting Url Keys.
57+
* @throws UrlAlreadyExistsException
5758
*/
58-
public function findUrlKeyConflicts(Product $product): array
59+
public function validateUrlKey(Product $product): void
5960
{
6061
if (!$product->getUrlKey()) {
6162
$urlKey = $this->productUrlPathGenerator->getUrlKey($product);
@@ -67,7 +68,7 @@ public function findUrlKeyConflicts(Product $product): array
6768
$storeIdsToPathForSave = [];
6869
$searchData = [
6970
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
70-
UrlRewrite::REQUEST_PATH => []
71+
UrlRewrite::REQUEST_PATH => [],
7172
];
7273

7374
foreach ($stores as $store) {
@@ -81,17 +82,25 @@ public function findUrlKeyConflicts(Product $product): array
8182
}
8283

8384
$urlRewrites = $this->urlFinder->findAllByData($searchData);
84-
$conflicts = [];
85+
$exceptionData = [];
8586

8687
foreach ($urlRewrites as $urlRewrite) {
8788
if (in_array($urlRewrite->getRequestPath(), $storeIdsToPathForSave)
8889
&& isset($storeIdsToPathForSave[$urlRewrite->getStoreId()])
89-
&& $storeIdsToPathForSave[$urlRewrite->getStoreId()] == $urlRewrite->getRequestPath()
90-
&& $product->getId() != $urlRewrite->getEntityId()) {
91-
$conflicts[] = $urlRewrite;
90+
&& $storeIdsToPathForSave[$urlRewrite->getStoreId()] === $urlRewrite->getRequestPath()
91+
&& $product->getId() !== $urlRewrite->getEntityId()
92+
) {
93+
$exceptionData[$urlRewrite->getUrlRewriteId()] = $urlRewrite->toArray();
9294
}
9395
}
9496

95-
return $conflicts;
97+
if ($exceptionData) {
98+
throw new UrlAlreadyExistsException(
99+
__('URL key for specified store already exists.'),
100+
null,
101+
0,
102+
$exceptionData
103+
);
104+
}
96105
}
97106
}

0 commit comments

Comments
 (0)