/** * Set redirect url for store view based on request path info * * @param \Magento\Store\Block\Switcher $switcher * @param \Magento\Store\Model\Store $store * @param array $data * @return array * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeGetTargetStorePostData(\Magento\Store\Block\Switcher $switcher, \Magento\Store\Model\Store $store, $data = []) { $urlRewrite = $this->urlFinder->findOneByData([UrlRewrite::TARGET_PATH => $this->trimSlashInPath($this->request->getPathInfo()), UrlRewrite::STORE_ID => $store->getId()]); if ($urlRewrite) { $data[ActionInterface::PARAM_NAME_URL_ENCODED] = $this->urlHelper->getEncodedUrl($this->trimSlashInPath($this->urlBuilder->getUrl($urlRewrite->getRequestPath()))); } return [$store, $data]; }
/** * 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 product you chose is not associated with the selected store or category.') : __('The category you chose is not associated with the selected store.'); throw new LocalizedException($message); } $targetPath = $rewrite->getRequestPath(); } return $targetPath; }
/** * Retrieve Product URL using UrlDataObject * * @param \Magento\Catalog\Model\Product $product * @param array $params * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getUrl(\Magento\Catalog\Model\Product $product, $params = []) { $routePath = ''; $routeParams = $params; $storeId = $product->getStoreId(); if (isset($params['_ignore_category'])) { unset($params['_ignore_category']); $categoryId = null; } else { $categoryId = $product->getCategoryId() && !$product->getDoNotUseCategoryId() ? $product->getCategoryId() : null; } if ($product->hasUrlDataObject()) { $requestPath = $product->getUrlDataObject()->getUrlRewrite(); $routeParams['_scope'] = $product->getUrlDataObject()->getStoreId(); } else { $requestPath = $product->getRequestPath(); if (empty($requestPath) && $requestPath !== false) { $filterData = [UrlRewrite::ENTITY_ID => $product->getId(), UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::STORE_ID => $storeId]; if ($categoryId) { $filterData[UrlRewrite::METADATA]['category_id'] = $categoryId; } $rewrite = $this->urlFinder->findOneByData($filterData); if ($rewrite) { $requestPath = $rewrite->getRequestPath(); $product->setRequestPath($requestPath); } else { $product->setRequestPath(false); } } } if (isset($routeParams['_scope'])) { $storeId = $this->_storeManager->getStore($routeParams['_scope'])->getId(); } if ($storeId != $this->_storeManager->getStore()->getId()) { $routeParams['_scope_to_url'] = true; } if (!empty($requestPath)) { $routeParams['_direct'] = $requestPath; } else { $routePath = 'catalog/product/view'; $routeParams['id'] = $product->getId(); $routeParams['s'] = $product->getUrlKey(); if ($categoryId) { $routeParams['category'] = $categoryId; } } // reset cached URL instance GET query params if (!isset($routeParams['_query'])) { $routeParams['_query'] = []; } return $this->getUrlInstance()->setScope($storeId)->getUrl($routePath, $routeParams); }
/** * Prepare url using passed id path and return it * or return false if path was not found in url rewrites. * * @throws \RuntimeException * @return string|false * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getHref() { if ($this->_href === null) { if (!$this->getData('id_path')) { throw new \RuntimeException('Parameter id_path is not set.'); } $rewriteData = $this->parseIdPath($this->getData('id_path')); $href = false; $store = $this->hasStoreId() ? $this->_storeManager->getStore($this->getStoreId()) : $this->_storeManager->getStore(); $filterData = [UrlRewrite::ENTITY_ID => $rewriteData[1], UrlRewrite::ENTITY_TYPE => $rewriteData[0], UrlRewrite::STORE_ID => $store->getId()]; if (!empty($rewriteData[2]) && $rewriteData[0] == ProductUrlRewriteGenerator::ENTITY_TYPE) { $filterData[UrlRewrite::METADATA]['category_id'] = $rewriteData[2]; } $rewrite = $this->urlFinder->findOneByData($filterData); if ($rewrite) { $href = $store->getUrl('', ['_direct' => $rewrite->getRequestPath()]); if (strpos($href, '___store') === false) { $href .= (strpos($href, '?') === false ? '?' : '&') . '___store=' . $store->getCode(); } } $this->_href = $href; } return $this->_href; }
/** * Get category url * * @return string */ public function getUrl() { $url = $this->_getData('url'); if ($url === null) { Profiler::start('REWRITE: ' . __METHOD__, ['group' => 'REWRITE', 'method' => __METHOD__]); if ($this->hasData('request_path') && $this->getRequestPath() != '') { $this->setData('url', $this->getUrlInstance()->getDirectUrl($this->getRequestPath())); Profiler::stop('REWRITE: ' . __METHOD__); return $this->getData('url'); } $rewrite = $this->urlFinder->findOneByData([UrlRewrite::ENTITY_ID => $this->getId(), UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::STORE_ID => $this->getStoreId()]); if ($rewrite) { $this->setData('url', $this->getUrlInstance()->getDirectUrl($rewrite->getRequestPath())); Profiler::stop('REWRITE: ' . __METHOD__); return $this->getData('url'); } $this->setData('url', $this->getCategoryIdUrl()); Profiler::stop('REWRITE: ' . __METHOD__); return $this->getData('url'); } return $url; }
/** * @param string $requestPath * @param int $storeId * @return UrlRewrite|null */ protected function getRewrite($requestPath, $storeId) { return $this->urlFinder->findOneByData([UrlRewrite::REQUEST_PATH => trim($requestPath, '/'), UrlRewrite::STORE_ID => $storeId]); }