/** * 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; }
/** * 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; }
/** * @dataProvider generatePathDataProvider */ public function testGeneratePath($type, $product, $category, $parentPath, $result) { $this->assertEquals($result, $this->_model->generatePath($type, $product, $category, $parentPath)); }