3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \CatalogUrlRewrite \Observer ;
7
9
8
10
use Magento \Catalog \Model \Category ;
9
11
use Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator ;
10
12
use Magento \CatalogUrlRewrite \Service \V1 \StoreViewService ;
11
13
use Magento \Catalog \Api \CategoryRepositoryInterface ;
12
- use Magento \Framework \Event \Observer ;
13
14
use Magento \CatalogUrlRewrite \Model \Category \ChildrenCategoriesProvider ;
14
15
use Magento \Framework \Event \ObserverInterface ;
15
16
use Magento \Store \Model \Store ;
19
20
*/
20
21
class CategoryUrlPathAutogeneratorObserver implements ObserverInterface
21
22
{
23
+
24
+ /**
25
+ * Reserved endpoint names.
26
+ *
27
+ * @var array
28
+ */
29
+ private $ invalidValues = ['admin ' , 'soap ' , 'rest ' , 'graphql ' ];
30
+
22
31
/**
23
32
* @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator
24
33
*/
@@ -72,7 +81,7 @@ public function execute(\Magento\Framework\Event\Observer $observer)
72
81
if ($ category ->getUrlKey () !== false && !$ useDefaultAttribute ) {
73
82
$ resultUrlKey = $ this ->categoryUrlPathGenerator ->getUrlKey ($ category );
74
83
$ this ->updateUrlKey ($ category , $ resultUrlKey );
75
- } else if ($ useDefaultAttribute ) {
84
+ } elseif ($ useDefaultAttribute ) {
76
85
$ resultUrlKey = $ category ->formatUrlKey ($ category ->getOrigData ('name ' ));
77
86
$ this ->updateUrlKey ($ category , $ resultUrlKey );
78
87
$ category ->setUrlKey (null )->setUrlPath (null );
@@ -92,6 +101,17 @@ private function updateUrlKey($category, $urlKey)
92
101
if (empty ($ urlKey )) {
93
102
throw new \Magento \Framework \Exception \LocalizedException (__ ('Invalid URL key ' ));
94
103
}
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
+
95
115
$ category ->setUrlKey ($ urlKey )
96
116
->setUrlPath ($ this ->categoryUrlPathGenerator ->getUrlPath ($ category ));
97
117
if (!$ category ->isObjectNew ()) {
0 commit comments