/** * Refresh product rewrites * * @param \Magento\Framework\Object $object * @return $this */ public function afterSave($object) { if ($object->dataHasChangedFor($this->getAttribute()->getName())) { $this->_catalogUrl->refreshProductRewrites(null, $object, true); } return $this; }
/** * Form post init * * @param \Magento\Framework\Data\Form $form * @return $this */ protected function _formPostInit($form) { $form->setAction($this->_adminhtmlData->getUrl('adminhtml/*/save', array('id' => $this->_getModel()->getId(), 'product' => $this->_getProduct()->getId(), 'category' => $this->_getCategory()->getId()))); /** @var $requestPath \Magento\Framework\Data\Form\Element\AbstractElement */ $requestPath = $this->getForm()->getElement('request_path'); /** @var $targetPath \Magento\Framework\Data\Form\Element\AbstractElement */ $targetPath = $this->getForm()->getElement('target_path'); $model = $this->_getModel(); $disablePaths = false; if (!$model->getId()) { $product = null; $category = null; if ($this->_getProduct()->getId()) { $product = $this->_getProduct(); $category = $this->_getCategory(); } elseif ($this->_getCategory()->getId()) { $category = $this->_getCategory(); } if ($product || $category) { $sessionData = $this->_getSessionData(); if (!isset($sessionData['request_path'])) { $requestPath->setValue($this->_catalogUrl->generatePath('request', $product, $category, '')); } $targetPath->setValue($this->_catalogUrl->generatePath('target', $product, $category)); $disablePaths = true; } } else { $disablePaths = in_array($model->getEntityType(), [UrlRedirect::ENTITY_TYPE_PRODUCT, UrlRedirect::ENTITY_TYPE_CATEGORY, UrlRedirect::ENTITY_TYPE_CMS_PAGE]); } if ($disablePaths) { $targetPath->setData('disabled', true); } return $this; }
/** * Rebuild all index data * * @return void * @throws \Exception */ public function reindexAll() { $this->_catalogResourceUrl->beginTransaction(); try { $this->_catalogUrl->refreshRewrites(); $this->_catalogResourceUrl->commit(); } catch (\Exception $e) { $this->_catalogResourceUrl->rollBack(); throw $e; } }
/** * Form post init * * @param \Magento\Framework\Data\Form $form * @return \Magento\Backend\Block\Urlrewrite\Catalog\Edit\Form */ protected function _formPostInit($form) { // Set form action $form->setAction($this->_adminhtmlData->getUrl('adminhtml/*/save', array('id' => $this->_getModel()->getId(), 'product' => $this->_getProduct()->getId(), 'category' => $this->_getCategory()->getId()))); // Fill id path, request path and target path elements /** @var $idPath \Magento\Framework\Data\Form\Element\AbstractElement */ $idPath = $this->getForm()->getElement('id_path'); /** @var $requestPath \Magento\Framework\Data\Form\Element\AbstractElement */ $requestPath = $this->getForm()->getElement('request_path'); /** @var $targetPath \Magento\Framework\Data\Form\Element\AbstractElement */ $targetPath = $this->getForm()->getElement('target_path'); $model = $this->_getModel(); $disablePaths = false; if (!$model->getId()) { $product = null; $category = null; if ($this->_getProduct()->getId()) { $product = $this->_getProduct(); $category = $this->_getCategory(); } elseif ($this->_getCategory()->getId()) { $category = $this->_getCategory(); } if ($product || $category) { $idPath->setValue($this->_catalogUrl->generatePath('id', $product, $category)); $sessionData = $this->_getSessionData(); if (!isset($sessionData['request_path'])) { $requestPath->setValue($this->_catalogUrl->generatePath('request', $product, $category, '')); } $targetPath->setValue($this->_catalogUrl->generatePath('target', $product, $category)); $disablePaths = true; } } else { $disablePaths = $model->getProductId() || $model->getCategoryId(); } // Disable id_path and target_path elements if ($disablePaths) { $idPath->setData('disabled', true); $targetPath->setData('disabled', true); } return $this; }
/** * @param string $targetPathExecute * @param bool $changeRequestPath * * @dataProvider refreshcategoryRewriteDataProvider */ public function testRefreshCategoryRewrite($targetPathExecute, $changeRequestPath) { $categoryId = 1; $rewrite = array('category/1' => $this->_rewriteModel); $this->_resourceModel->expects($this->once())->method('prepareRewrites')->will($this->returnValue($rewrite)); $this->_resourceModel->expects($this->at(0))->method('getStores')->will($this->returnValue(array($this->_storeModel))); $this->_resourceModel->expects($this->once())->method('getStores')->will($this->returnValue($this->_storeModel)); $this->_resourceModel->expects($this->any())->method('getCategoryModel')->will($this->returnValue($this->_categoryModel)); $this->_resourceModel->expects($this->once())->method('getCategoryParentPath')->will($this->returnValue('parent_path')); $this->_resourceModel->expects($this->any())->method('deleteRewriteRecord')->will($this->returnSelf()); $this->_resourceModel->expects($this->any())->method('saveCategoryAttribute')->will($this->returnSelf()); $this->_resourceModel->expects($this->any())->method('getProductsByCategory')->will($this->returnValue(null)); $this->_resourceModel->expects($this->any())->method('deleteCategoryProductStoreRewrites')->will($this->returnSelf()); $this->_resourceModel->expects($this->{$targetPathExecute}())->method('findFinalTargetPath')->will($this->returnValue('category/1')); $this->_categoryModel->expects($this->any())->method('getAllChilds')->will($this->returnValue(array($this->_categoryModel))); $this->_categoryModel->expects($this->any())->method('formatUrlKey')->will($this->returnValue('url_formatted')); $this->_categoryModel->expects($this->any())->method('getUrlKey')->will($this->returnValue('url_key')); $this->_categoryModel->expects($this->any())->method('getCategoryUrlPath')->will($this->returnValue('category_parent_path')); $this->_categoryHelper->expects($this->once())->method('getCategoryUrlPath')->will($this->returnValue('category_parent_path')); $this->_categoryHelper->expects($this->once())->method('getCategoryUrlSuffix')->will($this->returnValue('suffics')); $this->_rewriteModel->expects($this->once())->method('getRequestPath')->will($this->returnValue('category_parent_pathurl_formatted-1suffics')); $this->_model->refreshCategoryRewrite($categoryId, '', true, $changeRequestPath); }
/** * @dataProvider generatePathDataProvider */ public function testGeneratePath($type, $product, $category, $parentPath, $result) { $this->assertEquals($result, $this->_model->generatePath($type, $product, $category, $parentPath)); }