/** * Retrieve Product Url path (with category if exists) * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Category $category * * @return string */ public function getUrlPath($product, $category = null) { $path = $product->getData('url_path'); if ($path === null) { $path = $product->getUrlKey() === false ? $this->prepareProductDefaultUrlKey($product) : $this->prepareProductUrlKey($product); } return $category === null ? $path : $this->categoryUrlPathGenerator->getUrlPath($category) . '/' . $path; }
/** * @param Category $category * @return void */ protected function updateUrlPathForChildren($category) { foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { $childCategory->unsUrlPath(); $childCategory->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($childCategory)); $childCategory->getResource()->saveAttribute($childCategory, 'url_path'); } }
/** * Retrieve Product Url path (with category if exists) * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Category $category * * @return string */ public function getUrlPath($product, $category = null) { $path = $product->getData('url_path'); if ($path === null) { $path = $this->generateUrlKey($product); } return $category === null ? $path : $this->categoryUrlPathGenerator->getUrlPath($category) . '/' . $path; }
/** * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url * @param int $storeId * @return array */ protected function generateForCustom($url, $storeId) { $urls = []; $targetPath = !$url->getRedirectType() ? $url->getTargetPath() : $this->categoryUrlPathGenerator->getUrlPathWithSuffix($this->category, $storeId); if ($url->getRequestPath() !== $targetPath) { $urls[$url->getRequestPath() . '_' . $storeId] = $this->urlRewriteFactory->create()->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($this->category->getId())->setRequestPath($url->getRequestPath())->setTargetPath($targetPath)->setRedirectType($url->getRedirectType())->setStoreId($storeId)->setDescription($url->getDescription())->setIsAutogenerated(0)->setMetadata($url->getMetadata()); } return $urls; }
public function testGetUrlPathWithSuffixAndCategoryAnsStore() { $storeId = 1; $this->product->expects($this->once())->method('getData')->with('url_path')->will($this->returnValue('product-path')); $this->categoryUrlPathGenerator->expects($this->once())->method('getUrlPath')->will($this->returnValue('category-url-path')); $this->storeManager->expects($this->never())->method('getStore'); $this->scopeConfig->expects($this->once())->method('getValue')->with(ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX, \Magento\Framework\Store\ScopeInterface::SCOPE_STORE, $storeId)->will($this->returnValue('.html')); $this->assertEquals('category-url-path/product-path.html', $this->productUrlPathGenerator->getUrlPathWithSuffix($this->product, $storeId, $this->category)); }
/** * @dataProvider getUrlKeyDataProvider * @param string|null|bool $urlKey * @param string|null|bool $name * @param string $result */ public function testGetUrlKey($urlKey, $name, $result) { $this->category->expects($this->once())->method('getUrlKey')->will($this->returnValue($urlKey)); $this->category->expects($this->any())->method('getName')->will($this->returnValue($name)); $this->category->expects($this->once())->method('formatUrlKey')->will($this->returnArgument(0)); $this->assertEquals($result, $this->categoryUrlPathGenerator->getUrlKey($this->category)); }
public function testGenerate() { $requestPath = 'category.html'; $targetPath = 'target-path'; $storeId = 'store_id'; $categoryId = 'category_id'; $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId)); $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->will($this->returnValue($requestPath)); $this->categoryUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')->will($this->returnValue($targetPath)); $this->urlRewrite->expects($this->any())->method('setStoreId')->with($storeId)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setEntityId')->with($categoryId)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setEntityType')->with(CategoryUrlRewriteGenerator::ENTITY_TYPE)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setRequestPath')->with($requestPath)->will($this->returnSelf()); $this->urlRewrite->expects($this->any())->method('setTargetPath')->with($targetPath)->will($this->returnSelf()); $this->urlRewriteFactory->expects($this->any())->method('create')->will($this->returnValue($this->urlRewrite)); $this->assertEquals(['category.html_store_id' => $this->urlRewrite], $this->canonicalUrlRewriteGenerator->generate($storeId, $this->category)); }
/** * @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); }
public function testGenerationForCustomWithTargetPathGeneration() { $storeId = 12; $categoryId = 123; $requestPath = 'generate-for-custom-without-redirect-type.html'; $targetPath = 'generated-target-path.html'; $description = 'description'; $this->urlFinder->expects($this->once())->method('findAllByData')->will($this->returnValue($this->getCurrentRewritesMocks([[UrlRewrite::REQUEST_PATH => $requestPath, UrlRewrite::TARGET_PATH => 'custom-target-path.html', UrlRewrite::REDIRECT_TYPE => 'code', UrlRewrite::IS_AUTOGENERATED => 0, UrlRewrite::DESCRIPTION => $description, UrlRewrite::METADATA => []]]))); $this->categoryUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->will($this->returnValue($targetPath)); $this->category->expects($this->any())->method('getId')->will($this->returnValue($categoryId)); $this->urlRewriteBuilder->expects($this->once())->method('setDescription')->with($description)->will($this->returnSelf()); $this->prepareUrlRewriteMock($storeId, $categoryId, $requestPath, $targetPath, 'code'); $this->assertEquals([$this->urlRewrite], $this->currentUrlRewritesRegenerator->generate($storeId, $this->category)); }
/** * Category view action * * @return \Magento\Framework\Controller\ResultInterface * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function execute() { if ($this->_request->getParam(\Magento\Framework\App\ActionInterface::PARAM_NAME_URL_ENCODED)) { return $this->resultRedirectFactory->create()->setUrl($this->_redirect->getRedirectUrl()); } $category = $this->_initCategory(); if ($category) { $this->layerResolver->create(Resolver::CATALOG_LAYER_CATEGORY); $settings = $this->_catalogDesign->getDesignSettings($category); // apply custom design if ($settings->getCustomDesign()) { $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign()); } $this->_catalogSession->setLastViewedCategoryId($category->getId()); $page = $this->resultPageFactory->create(); // apply custom layout (page) template once the blocks are generated if ($settings->getPageLayout()) { $page->getConfig()->setPageLayout($settings->getPageLayout()); } $hasChildren = $category->hasChildren(); if ($category->getIsAnchor()) { $type = $hasChildren ? 'layered' : 'layered_without_children'; } else { $type = $hasChildren ? 'default' : 'default_without_children'; } if (!$hasChildren) { // Two levels removed from parent. Need to add default page type. $parentType = strtok($type, '_'); $page->addPageLayoutHandles(['type' => $parentType]); } $page->addPageLayoutHandles(['type' => $type, 'id' => $category->getId()]); // apply custom layout update once layout is loaded $layoutUpdates = $settings->getLayoutUpdates(); if ($layoutUpdates && is_array($layoutUpdates)) { foreach ($layoutUpdates as $layoutUpdate) { $page->addUpdate($layoutUpdate); $page->addPageLayoutHandles(['layout_update' => md5($layoutUpdate)]); } } $page->getConfig()->addBodyClass('page-products')->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($category))->addBodyClass('category-' . $category->getUrlKey()); return $page; } elseif (!$this->getResponse()->isRedirect()) { return $this->resultForwardFactory->create()->forward('noroute'); } }
/** * Init layout for viewing product page * * @param \Magento\Framework\View\Result\Page $resultPage * @param \Magento\Catalog\Model\Product $product * @param null|\Magento\Framework\Object $params * @return \Magento\Catalog\Helper\Product\View * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function initProductLayout(ResultPage $resultPage, $product, $params = null) { $settings = $this->_catalogDesign->getDesignSettings($product); $pageConfig = $resultPage->getConfig(); if ($settings->getCustomDesign()) { $this->_catalogDesign->applyCustomDesign($settings->getCustomDesign()); } // Apply custom page layout if ($settings->getPageLayout()) { $pageConfig->setPageLayout($settings->getPageLayout()); } $urlSafeSku = rawurlencode($product->getSku()); // Load default page handles and page configurations if ($params && $params->getBeforeHandles()) { foreach ($params->getBeforeHandles() as $handle) { $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()], $handle); } } $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()]); if ($params && $params->getAfterHandles()) { foreach ($params->getAfterHandles() as $handle) { $resultPage->addPageLayoutHandles(['id' => $product->getId(), 'sku' => $urlSafeSku, 'type' => $product->getTypeId()], $handle); } } // Apply custom layout update once layout is loaded $update = $resultPage->getLayout()->getUpdate(); $layoutUpdates = $settings->getLayoutUpdates(); if ($layoutUpdates) { if (is_array($layoutUpdates)) { foreach ($layoutUpdates as $layoutUpdate) { $update->addUpdate($layoutUpdate); } } } $currentCategory = $this->_coreRegistry->registry('current_category'); $controllerClass = $this->_request->getFullActionName(); if ($controllerClass != 'catalog-product-view') { $pageConfig->addBodyClass('catalog-product-view'); } $pageConfig->addBodyClass('product-' . $product->getUrlKey()); if ($currentCategory instanceof \Magento\Catalog\Model\Category) { $pageConfig->addBodyClass('categorypath-' . $this->categoryUrlPathGenerator->getUrlPath($currentCategory))->addBodyClass('category-' . $currentCategory->getUrlKey()); } return $this; }
/** * Generate list based on store view * * @param int $storeId * @param \Magento\Catalog\Model\Category $category * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ public function generate($storeId, Category $category) { $urlPath = $this->categoryUrlPathGenerator->getUrlPathWithSuffix($category, $storeId); $result = [$urlPath . '_' . $storeId => $this->urlRewriteFactory->create()->setStoreId($storeId)->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($category->getId())->setRequestPath($urlPath)->setTargetPath($this->categoryUrlPathGenerator->getCanonicalUrlPath($category))]; return $result; }
/** * @param \Magento\Catalog\Model\Product|null $product * @param \Magento\Catalog\Model\Category|null $category * @return string */ protected function getTargetPath($product = null, $category = null) { return $product ? $this->productUrlPathGenerator->getCanonicalUrlPath($product, $category) : $this->categoryUrlPathGenerator->getCanonicalUrlPath($category); }
/** * @param Category $category * @return void */ protected function updateUrlPathForCategory(Category $category) { $category->unsUrlPath(); $category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category)); $category->getResource()->saveAttribute($category, 'url_path'); }