12
12
use Magento \Catalog \Controller \Adminhtml \Product ;
13
13
use Magento \Framework \App \ObjectManager ;
14
14
use Magento \Store \Model \StoreManagerInterface ;
15
+ use Magento \UrlRewrite \Model \Exception \UrlAlreadyExistsException ;
16
+ use Magento \Catalog \Model \Product as ProductModel ;
17
+ use Magento \CatalogUrlRewrite \Model \Product \Validator as ProductUrlRewriteValidator ;
15
18
16
19
/**
17
20
* Product validate
@@ -57,6 +60,11 @@ class Validate extends Product implements HttpPostActionInterface, HttpGetAction
57
60
*/
58
61
private $ storeManager ;
59
62
63
+ /**
64
+ * @var ProductUrlRewriteValidator
65
+ */
66
+ private $ productUrlRewriteValidator ;
67
+
60
68
/**
61
69
* @param Action\Context $context
62
70
* @param Builder $productBuilder
@@ -65,6 +73,7 @@ class Validate extends Product implements HttpPostActionInterface, HttpGetAction
65
73
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
66
74
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
67
75
* @param \Magento\Catalog\Model\ProductFactory $productFactory
76
+ * @param ProductUrlRewriteValidator $productUrlRewriteValidator
68
77
*/
69
78
public function __construct (
70
79
\Magento \Backend \App \Action \Context $ context ,
@@ -73,14 +82,16 @@ public function __construct(
73
82
\Magento \Catalog \Model \Product \Validator $ productValidator ,
74
83
\Magento \Framework \Controller \Result \JsonFactory $ resultJsonFactory ,
75
84
\Magento \Framework \View \LayoutFactory $ layoutFactory ,
76
- \Magento \Catalog \Model \ProductFactory $ productFactory
85
+ \Magento \Catalog \Model \ProductFactory $ productFactory ,
86
+ ProductUrlRewriteValidator $ productUrlRewriteValidator
77
87
) {
78
88
$ this ->_dateFilter = $ dateFilter ;
79
89
$ this ->productValidator = $ productValidator ;
80
90
parent ::__construct ($ context , $ productBuilder );
81
91
$ this ->resultJsonFactory = $ resultJsonFactory ;
82
92
$ this ->layoutFactory = $ layoutFactory ;
83
93
$ this ->productFactory = $ productFactory ;
94
+ $ this ->productUrlRewriteValidator = $ productUrlRewriteValidator ;
84
95
}
85
96
86
97
/**
@@ -130,11 +141,18 @@ public function execute()
130
141
$ resource ->getAttribute ('news_from_date ' )->setMaxValue ($ product ->getNewsToDate ());
131
142
$ resource ->getAttribute ('custom_design_from ' )->setMaxValue ($ product ->getCustomDesignTo ());
132
143
144
+ $ this ->validateUrlKeyUniqueness ($ product );
133
145
$ this ->productValidator ->validate ($ product , $ this ->getRequest (), $ response );
134
146
} catch (\Magento \Eav \Model \Entity \Attribute \Exception $ e ) {
135
147
$ response ->setError (true );
136
148
$ response ->setAttribute ($ e ->getAttributeCode ());
137
149
$ response ->setMessages ([$ e ->getMessage ()]);
150
+ } catch (UrlAlreadyExistsException $ e ) {
151
+ $ this ->messageManager ->addExceptionMessage ($ e );
152
+ $ layout = $ this ->layoutFactory ->create ();
153
+ $ layout ->initMessages ();
154
+ $ response ->setError (true );
155
+ $ response ->setHtmlMessage ($ layout ->getMessagesBlock ()->getGroupedHtml ());
138
156
} catch (\Magento \Framework \Exception \LocalizedException $ e ) {
139
157
$ response ->setError (true );
140
158
$ response ->setMessages ([$ e ->getMessage ()]);
@@ -149,6 +167,32 @@ public function execute()
149
167
return $ this ->resultJsonFactory ->create ()->setData ($ response );
150
168
}
151
169
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
+
152
196
/**
153
197
* @return StoreManagerInterface
154
198
* @deprecated 101.0.0
0 commit comments