Skip to content

Commit 7c86433

Browse files
MAGETWO-69825: [GITHUB #9891] Subcategory "liquid-hand-soap" is not opened in category "soap"
- Implemented validation URL key during category creation
1 parent f7d4822 commit 7c86433

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

app/code/Magento/CatalogUrlRewrite/Observer/CategoryUrlPathAutogeneratorObserver.php

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

810
use Magento\Catalog\Model\Category;
911
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
1012
use Magento\CatalogUrlRewrite\Service\V1\StoreViewService;
1113
use Magento\Catalog\Api\CategoryRepositoryInterface;
12-
use Magento\Framework\Event\Observer;
1314
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
1415
use Magento\Framework\Event\ObserverInterface;
1516
use Magento\Store\Model\Store;
@@ -19,6 +20,14 @@
1920
*/
2021
class CategoryUrlPathAutogeneratorObserver implements ObserverInterface
2122
{
23+
24+
/**
25+
* Reserved endpoint names.
26+
*
27+
* @var array
28+
*/
29+
private $invalidValues = ['admin', 'soap', 'rest', 'graphql'];
30+
2231
/**
2332
* @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator
2433
*/
@@ -72,7 +81,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
7281
if ($category->getUrlKey() !== false && !$useDefaultAttribute) {
7382
$resultUrlKey = $this->categoryUrlPathGenerator->getUrlKey($category);
7483
$this->updateUrlKey($category, $resultUrlKey);
75-
} else if ($useDefaultAttribute) {
84+
} elseif ($useDefaultAttribute) {
7685
$resultUrlKey = $category->formatUrlKey($category->getOrigData('name'));
7786
$this->updateUrlKey($category, $resultUrlKey);
7887
$category->setUrlKey(null)->setUrlPath(null);
@@ -92,6 +101,17 @@ private function updateUrlKey($category, $urlKey)
92101
if (empty($urlKey)) {
93102
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid URL key'));
94103
}
104+
105+
if (in_array($urlKey, $this->invalidValues)) {
106+
throw new \Magento\Framework\Exception\LocalizedException(
107+
__(
108+
'URL key "%1" conflicts with reserved endpoint names: %2. Try another url key.',
109+
$urlKey,
110+
implode(', ', $this->invalidValues)
111+
)
112+
);
113+
}
114+
95115
$category->setUrlKey($urlKey)
96116
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
97117
if (!$category->isObjectNew()) {

0 commit comments

Comments
 (0)