1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite;
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
class Save extends \Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite implements HttpPostActionInterface
{
/**
* @var \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator
*/
protected $productUrlPathGenerator;
/**
* @var \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator
*/
protected $categoryUrlPathGenerator;
/**
* @var \Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator
*/
protected $cmsPageUrlPathGenerator;
/**
* @var \Magento\UrlRewrite\Model\UrlFinderInterface
*/
protected $urlFinder;
/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $productUrlPathGenerator
* @param \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator
* @param \Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $cmsPageUrlPathGenerator
* @param UrlFinderInterface $urlFinder
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $productUrlPathGenerator,
\Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
\Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $cmsPageUrlPathGenerator,
UrlFinderInterface $urlFinder
) {
parent::__construct($context);
$this->productUrlPathGenerator = $productUrlPathGenerator;
$this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
$this->cmsPageUrlPathGenerator = $cmsPageUrlPathGenerator;
$this->urlFinder = $urlFinder;
}
/**
* Override urlrewrite data, basing on current category and product
*
* @param \Magento\UrlRewrite\Model\UrlRewrite $model
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _handleCatalogUrlRewrite($model)
{
$productId = $this->_getProduct()->getId();
$categoryId = $this->_getCategory()->getId();
if ($productId || $categoryId) {
if ($model->isObjectNew()) {
$model->setEntityType($productId ? self::ENTITY_TYPE_PRODUCT : self::ENTITY_TYPE_CATEGORY)
->setEntityId($productId ?: $categoryId);
if ($productId && $categoryId) {
$model->setMetadata(['category_id' => $categoryId]);
}
}
$model->setTargetPath($this->getTargetPath($model));
}
}
/**
* Get Target Path
*
* @param \Magento\UrlRewrite\Model\UrlRewrite $model
* @return string
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function getTargetPath($model)
{
$targetPath = $this->getCanonicalTargetPath();
if ($model->getRedirectType() && !$model->getIsAutogenerated()) {
$data = [
UrlRewrite::ENTITY_ID => $model->getEntityId(),
UrlRewrite::TARGET_PATH => $targetPath,
UrlRewrite::ENTITY_TYPE => $model->getEntityType(),
UrlRewrite::STORE_ID => $model->getStoreId(),
];
$rewrite = $this->urlFinder->findOneByData($data);
if (!$rewrite) {
$message = $model->getEntityType() === self::ENTITY_TYPE_PRODUCT
? __("The selected product isn't associated with the selected store or category.")
: __("The selected category isn't associated with the selected store.");
throw new LocalizedException($message);
}
$targetPath = $rewrite->getRequestPath();
}
return $targetPath;
}
/**
* @return string
*/
protected function getCanonicalTargetPath()
{
$product = $this->_getProduct()->getId() ? $this->_getProduct() : null;
$category = $this->_getCategory()->getId() ? $this->_getCategory() : null;
return $product
? $this->productUrlPathGenerator->getCanonicalUrlPath($product, $category)
: $this->categoryUrlPathGenerator->getCanonicalUrlPath($category);
}
/**
* Override URL rewrite data, basing on current CMS page
*
* @param \Magento\UrlRewrite\Model\UrlRewrite $model
* @return void
*/
private function _handleCmsPageUrlRewrite($model)
{
$cmsPage = $this->_getCmsPage();
if ($cmsPage->getId()) {
if ($model->isObjectNew()) {
$model->setEntityType(self::ENTITY_TYPE_CMS_PAGE)->setEntityId($cmsPage->getId());
}
if ($model->getRedirectType() && !$model->getIsAutogenerated()) {
$targetPath = $this->cmsPageUrlPathGenerator->getUrlPath($cmsPage);
} else {
$targetPath = $this->cmsPageUrlPathGenerator->getCanonicalUrlPath($cmsPage);
}
$model->setTargetPath($targetPath);
}
}
/**
* @return void
*/
public function execute()
{
$data = $this->getRequest()->getPostValue();
if ($data) {
/** @var $session \Magento\Backend\Model\Session */
$session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
try {
$model = $this->_getUrlRewrite();
$requestPath = $this->getRequest()->getParam('request_path');
$this->_objectManager->get(
\Magento\UrlRewrite\Helper\UrlRewrite::class
)->validateRequestPath($requestPath);
$model->setEntityType($this->getRequest()->getParam('entity_type') ?: self::ENTITY_TYPE_CUSTOM)
->setRequestPath($requestPath)
->setTargetPath($this->getRequest()->getParam('target_path', $model->getTargetPath()))
->setRedirectType($this->getRequest()->getParam('redirect_type'))
->setStoreId($this->getRequest()->getParam('store_id', 0))
->setDescription($this->getRequest()->getParam('description'));
$this->_handleCatalogUrlRewrite($model);
$this->_handleCmsPageUrlRewrite($model);
$model->save();
$this->messageManager->addSuccess(__('The URL Rewrite has been saved.'));
$this->_redirect('adminhtml/*/');
return;
} catch (LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
$session->setUrlRewriteData($data);
} catch (\Exception $e) {
$this->messageManager->addException(
$e,
__('An error occurred while saving the URL rewrite. Please try to save again.')
);
$session->setUrlRewriteData($data);
}
}
$this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($this->getUrl('*')));
}
}