public function testGetCategoryUrl() { $url = 'http://example.com/'; $category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Category', ['data' => ['url' => $url]]); $this->assertEquals($url, $this->_helper->getCategoryUrl($category)); $category = new \Magento\Framework\DataObject(['url' => $url]); $this->assertEquals($url, $this->_helper->getCategoryUrl($category)); }
/** * Get category data to be added to the Menu * * @param \Magento\Framework\Data\Tree\Node $category * @return array */ public function getMenuCategoryData($category) { $nodeId = 'category-node-' . $category->getId(); $isActiveCategory = false; /** @var \Magento\Catalog\Model\Category $currentCategory */ $currentCategory = $this->registry->registry('current_category'); if ($currentCategory && $currentCategory->getId() == $category->getId()) { $isActiveCategory = true; } $categoryData = ['name' => $category->getName(), 'id' => $nodeId, 'url' => $this->catalogCategory->getCategoryUrl($category), 'has_active' => $this->hasActive($category), 'is_active' => $isActiveCategory]; return $categoryData; }
/** * Recursively adds categories to top menu * * @param \Magento\Framework\Data\Tree\Node\Collection|array $categories * @param \Magento\Framework\Data\Tree\Node $parentCategoryNode * @param \Magento\Theme\Block\Html\Topmenu $block * @return void */ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block) { foreach ($categories as $category) { if (!$category->getIsActive()) { continue; } $nodeId = 'category-node-' . $category->getId(); $block->addIdentity(\Magento\Catalog\Model\Category::CACHE_TAG . '_' . $category->getId()); $tree = $parentCategoryNode->getTree(); $isActiveCategory = false; /** @var \Magento\Catalog\Model\Category $currentCategory */ $currentCategory = $this->_registry->registry('current_category'); if ($currentCategory && $currentCategory->getId() == $category->getId()) { $isActiveCategory = true; } $categoryData = ['name' => $category->getName(), 'id' => $nodeId, 'url' => $this->_catalogCategory->getCategoryUrl($category), 'has_active' => $this->hasActive($category), 'is_active' => $isActiveCategory]; $categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode); $parentCategoryNode->addChild($categoryNode); if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource()) { $subcategories = (array) $category->getChildrenNodes(); } else { $subcategories = $category->getChildren(); } $this->_addCategoriesToMenu($subcategories, $categoryNode, $block); } }
/** * Recursively adds categories to top menu * * @param \Magento\Framework\Data\Tree\Node\Collection|array $categories * @param \Magento\Framework\Data\Tree\Node $parentCategoryNode * @param \Magento\Theme\Block\Html\Topmenu $block * @return void */ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block) { foreach ($categories as $category) { if (!$category->getIsActive()) { continue; } $nodeId = 'category-node-' . $category->getId(); $block->addIdentity(\Magento\Catalog\Model\Category::CACHE_TAG . '_' . $category->getId()); $tree = $parentCategoryNode->getTree(); $categoryData = array('name' => $category->getName(), 'id' => $nodeId, 'url' => $this->_catalogCategory->getCategoryUrl($category), 'is_active' => $this->_isActiveMenuCategory($category)); $categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode); $parentCategoryNode->addChild($categoryNode); if ($this->categoryFlatConfig->isFlatEnabled()) { $subcategories = (array) $category->getChildrenNodes(); } else { $subcategories = $category->getChildren(); } $this->_addCategoriesToMenu($subcategories, $categoryNode, $block); } }
/** * Return Category Id for $category object * * @param $category * * @return string */ public function getCategoryUrl($category) { return $this->_categoryHelper->getCategoryUrl($category); }
/** * Convert category to array * * @param \Magento\Catalog\Model\Category $category * @param \Magento\Catalog\Model\Category $currentCategory * @return array */ private function getCategoryAsArray($category, $currentCategory) { return ['name' => $category->getName(), 'id' => 'category-node-' . $category->getId(), 'url' => $this->catalogCategory->getCategoryUrl($category), 'has_active' => in_array((string) $category->getId(), explode('/', $currentCategory->getPath()), true), 'is_active' => $category->getId() == $currentCategory->getId()]; }