8
8
namespace Magento \UrlRewrite \Controller \Adminhtml \Url \Rewrite ;
9
9
10
10
use Magento \Backend \App \Action \Context ;
11
- use Magento \Catalog \Api \ProductRepositoryInterface ;
12
11
use Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator ;
12
+ use Magento \CatalogUrlRewrite \Model \Products \AppendUrlRewritesToProducts ;
13
13
use Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator ;
14
14
use Magento \CmsUrlRewrite \Model \CmsPageUrlPathGenerator ;
15
15
use Magento \Framework \App \Action \HttpPostActionInterface as HttpPostActionInterface ;
16
16
use Magento \Framework \Exception \LocalizedException ;
17
17
use Magento \UrlRewrite \Model \UrlFinderInterface ;
18
18
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
19
19
20
+ /**
21
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
22
+ */
20
23
class Save extends \Magento \UrlRewrite \Controller \Adminhtml \Url \Rewrite implements HttpPostActionInterface
21
24
{
22
25
/**
@@ -40,33 +43,33 @@ class Save extends \Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite implemen
40
43
protected $ urlFinder ;
41
44
42
45
/**
43
- * @var ProductRepositoryInterface
46
+ * @var AppendUrlRewritesToProducts
44
47
*/
45
- protected ProductRepositoryInterface $ productRepository ;
48
+ protected AppendUrlRewritesToProducts $ productAppendRewrites ;
46
49
47
50
/**
48
51
* @param Context $context
49
52
* @param ProductUrlPathGenerator $productUrlPathGenerator
50
53
* @param CategoryUrlPathGenerator $categoryUrlPathGenerator
51
54
* @param CmsPageUrlPathGenerator $cmsPageUrlPathGenerator
52
55
* @param UrlFinderInterface $urlFinder
53
- * @param ProductRepositoryInterface |null $productRepository
56
+ * @param AppendUrlRewritesToProducts |null $productAppendRewrites
54
57
*/
55
58
public function __construct (
56
- \Magento \Backend \App \Action \Context $ context ,
57
- \Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator $ productUrlPathGenerator ,
59
+ \Magento \Backend \App \Action \Context $ context ,
60
+ \Magento \CatalogUrlRewrite \Model \ProductUrlPathGenerator $ productUrlPathGenerator ,
58
61
\Magento \CatalogUrlRewrite \Model \CategoryUrlPathGenerator $ categoryUrlPathGenerator ,
59
- \Magento \CmsUrlRewrite \Model \CmsPageUrlPathGenerator $ cmsPageUrlPathGenerator ,
60
- UrlFinderInterface $ urlFinder ,
61
- ProductRepositoryInterface $ productRepository = null
62
+ \Magento \CmsUrlRewrite \Model \CmsPageUrlPathGenerator $ cmsPageUrlPathGenerator ,
63
+ UrlFinderInterface $ urlFinder ,
64
+ AppendUrlRewritesToProducts $ productAppendRewrites = null
62
65
) {
63
66
parent ::__construct ($ context );
64
67
$ this ->productUrlPathGenerator = $ productUrlPathGenerator ;
65
68
$ this ->categoryUrlPathGenerator = $ categoryUrlPathGenerator ;
66
69
$ this ->cmsPageUrlPathGenerator = $ cmsPageUrlPathGenerator ;
67
70
$ this ->urlFinder = $ urlFinder ;
68
- $ this ->productRepository = $ productRepository ?:
69
- \Magento \Framework \App \ObjectManager::getInstance ()->create (ProductRepositoryInterface ::class);
71
+ $ this ->productAppendRewrites = $ productAppendRewrites ?:
72
+ \Magento \Framework \App \ObjectManager::getInstance ()->create (AppendUrlRewritesToProducts ::class);
70
73
}
71
74
72
75
/**
@@ -88,18 +91,18 @@ protected function _handleCatalogUrlRewrite($model)
88
91
$ model ->setMetadata (['category_id ' => $ categoryId ]);
89
92
}
90
93
}
91
- $ model ->setTargetPath ($ this ->getTargetPath ($ model ));
94
+ $ model ->setTargetPath ($ this ->generateTargetPath ($ model ));
92
95
}
93
96
}
94
97
95
98
/**
96
- * Get Target Path
99
+ * Generate Target Path
97
100
*
98
101
* @param \Magento\UrlRewrite\Model\UrlRewrite $model
99
102
* @return string
100
103
* @throws \Magento\Framework\Exception\LocalizedException
101
104
*/
102
- protected function getTargetPath ($ model )
105
+ protected function generateTargetPath ($ model )
103
106
{
104
107
$ targetPath = $ this ->getCanonicalTargetPath ();
105
108
if ($ model ->getRedirectType () && !$ model ->getIsAutogenerated ()) {
@@ -111,14 +114,29 @@ protected function getTargetPath($model)
111
114
];
112
115
$ rewrite = $ this ->urlFinder ->findOneByData ($ data );
113
116
if (!$ rewrite ) {
114
- $ check = $ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT ?
115
- $ this ->_getProduct ()->canBeShowInCategory ($ this ->_getCategory ()->getId ()) :
116
- $ this ->_getCategory ()->getStoreId () == $ model ->getStoreId ();
117
- if (false === $ check ) {
118
- $ message = $ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT
119
- ? __ ("The selected product isn't associated with the selected store or category. " )
120
- : __ ("The selected category isn't associated with the selected store. " );
121
- throw new LocalizedException ($ message );
117
+ if ($ model ->getEntityType () === self ::ENTITY_TYPE_PRODUCT ) {
118
+ $ this ->productAppendRewrites ->execute (
119
+ [$ this ->_getProduct ()],
120
+ [$ this ->getRequest ()->getParam ('store_id ' , 0 )]
121
+ );
122
+ $ rewrite = $ this ->urlFinder ->findOneByData ($ data );
123
+ if (!$ rewrite ) {
124
+ throw new LocalizedException (
125
+ __ (
126
+ "The selected product isn't associated with the selected store or category. "
127
+ )
128
+ );
129
+ }
130
+ $ targetPath = $ rewrite ->getRequestPath ();
131
+ if ($ rewrite ->getRequestPath () == $ model ->getRequestPath () &&
132
+ $ rewrite ->getStoreId () == $ model ->getStoreId ()) {
133
+ $ obsoleteRewrite = $ this ->_objectManager ->create (\Magento \UrlRewrite \Model \UrlRewrite::class);
134
+ $ obsoleteRewrite ->load ($ rewrite ->getUrlRewriteId ());
135
+ $ obsoleteRewrite ->delete ();
136
+ }
137
+ } else {
138
+ throw new
139
+ LocalizedException (__ ("The selected category isn't associated with the selected store. " ));
122
140
}
123
141
} else {
124
142
$ targetPath = $ rewrite ->getRequestPath ();
0 commit comments