Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Save.php
Go to the documentation of this file.
1 <?php
9 
10 use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
14 
16 {
21 
26 
31 
35  protected $urlFinder;
36 
44  public function __construct(
45  \Magento\Backend\App\Action\Context $context,
46  \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $productUrlPathGenerator,
47  \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator,
48  \Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $cmsPageUrlPathGenerator,
50  ) {
51  parent::__construct($context);
52  $this->productUrlPathGenerator = $productUrlPathGenerator;
53  $this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
54  $this->cmsPageUrlPathGenerator = $cmsPageUrlPathGenerator;
55  $this->urlFinder = $urlFinder;
56  }
57 
65  protected function _handleCatalogUrlRewrite($model)
66  {
67  $productId = $this->_getProduct()->getId();
68  $categoryId = $this->_getCategory()->getId();
69  if ($productId || $categoryId) {
70  if ($model->isObjectNew()) {
71  $model->setEntityType($productId ? self::ENTITY_TYPE_PRODUCT : self::ENTITY_TYPE_CATEGORY)
72  ->setEntityId($productId ?: $categoryId);
73  if ($productId && $categoryId) {
74  $model->setMetadata(['category_id' => $categoryId]);
75  }
76  }
77  $model->setTargetPath($this->getTargetPath($model));
78  }
79  }
80 
88  protected function getTargetPath($model)
89  {
90  $targetPath = $this->getCanonicalTargetPath();
91  if ($model->getRedirectType() && !$model->getIsAutogenerated()) {
92  $data = [
93  UrlRewrite::ENTITY_ID => $model->getEntityId(),
94  UrlRewrite::TARGET_PATH => $targetPath,
95  UrlRewrite::ENTITY_TYPE => $model->getEntityType(),
96  UrlRewrite::STORE_ID => $model->getStoreId(),
97  ];
98  $rewrite = $this->urlFinder->findOneByData($data);
99  if (!$rewrite) {
100  $message = $model->getEntityType() === self::ENTITY_TYPE_PRODUCT
101  ? __("The selected product isn't associated with the selected store or category.")
102  : __("The selected category isn't associated with the selected store.");
103  throw new LocalizedException($message);
104  }
105  $targetPath = $rewrite->getRequestPath();
106  }
107  return $targetPath;
108  }
109 
113  protected function getCanonicalTargetPath()
114  {
115  $product = $this->_getProduct()->getId() ? $this->_getProduct() : null;
116  $category = $this->_getCategory()->getId() ? $this->_getCategory() : null;
117  return $product
118  ? $this->productUrlPathGenerator->getCanonicalUrlPath($product, $category)
119  : $this->categoryUrlPathGenerator->getCanonicalUrlPath($category);
120  }
121 
128  private function _handleCmsPageUrlRewrite($model)
129  {
130  $cmsPage = $this->_getCmsPage();
131  if ($cmsPage->getId()) {
132  if ($model->isObjectNew()) {
133  $model->setEntityType(self::ENTITY_TYPE_CMS_PAGE)->setEntityId($cmsPage->getId());
134  }
135  if ($model->getRedirectType() && !$model->getIsAutogenerated()) {
136  $targetPath = $this->cmsPageUrlPathGenerator->getUrlPath($cmsPage);
137  } else {
138  $targetPath = $this->cmsPageUrlPathGenerator->getCanonicalUrlPath($cmsPage);
139  }
140  $model->setTargetPath($targetPath);
141  }
142  }
143 
147  public function execute()
148  {
149  $data = $this->getRequest()->getPostValue();
150  if ($data) {
152  $session = $this->_objectManager->get(\Magento\Backend\Model\Session::class);
153  try {
154  $model = $this->_getUrlRewrite();
155 
156  $requestPath = $this->getRequest()->getParam('request_path');
157  $this->_objectManager->get(
158  \Magento\UrlRewrite\Helper\UrlRewrite::class
159  )->validateRequestPath($requestPath);
160 
161  $model->setEntityType($this->getRequest()->getParam('entity_type') ?: self::ENTITY_TYPE_CUSTOM)
162  ->setRequestPath($requestPath)
163  ->setTargetPath($this->getRequest()->getParam('target_path', $model->getTargetPath()))
164  ->setRedirectType($this->getRequest()->getParam('redirect_type'))
165  ->setStoreId($this->getRequest()->getParam('store_id', 0))
166  ->setDescription($this->getRequest()->getParam('description'));
167 
169  $this->_handleCmsPageUrlRewrite($model);
170  $model->save();
171 
172  $this->messageManager->addSuccess(__('The URL Rewrite has been saved.'));
173  $this->_redirect('adminhtml/*/');
174  return;
175  } catch (LocalizedException $e) {
176  $this->messageManager->addError($e->getMessage());
177  $session->setUrlRewriteData($data);
178  } catch (\Exception $e) {
179  $this->messageManager->addException(
180  $e,
181  __('An error occurred while saving the URL rewrite. Please try to save again.')
182  );
183  $session->setUrlRewriteData($data);
184  }
185  }
186  $this->getResponse()->setRedirect($this->_redirect->getRedirectUrl($this->getUrl('*')));
187  }
188 }
__()
Definition: __.php:13
$message
__construct(\Magento\Backend\App\Action\Context $context, \Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator $productUrlPathGenerator, \Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator $categoryUrlPathGenerator, \Magento\CmsUrlRewrite\Model\CmsPageUrlPathGenerator $cmsPageUrlPathGenerator, UrlFinderInterface $urlFinder)
Definition: Save.php:44