_categoryFactory = $categoryFactory; $this->_storeManager = $storeManager; $this->_dataCollectionFactory = $dataCollectionFactory; $this->categoryRepository = $categoryRepository; parent::__construct($context); } /** * Retrieve current store categories * * @param bool|string $sorted * @param bool $asCollection * @param bool $toLoad * @return \Magento\Framework\Data\Tree\Node\Collection or * \Magento\Catalog\Model\ResourceModel\Category\Collection or array */ public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true) { $parent = $this->_storeManager->getStore()->getRootCategoryId(); $cacheKey = sprintf('%d-%d-%d-%d', $parent, $sorted, $asCollection, $toLoad); if (isset($this->_storeCategories[$cacheKey])) { return $this->_storeCategories[$cacheKey]; } /** * Check if parent node of the store still exists */ $category = $this->_categoryFactory->create(); /* @var $category ModelCategory */ if (!$category->checkId($parent)) { if ($asCollection) { return $this->_dataCollectionFactory->create(); } return []; } $recursionLevel = max( 0, (int)$this->scopeConfig->getValue( 'catalog/navigation/max_depth', \Magento\Store\Model\ScopeInterface::SCOPE_STORE ) ); $storeCategories = $category->getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad); $this->_storeCategories[$cacheKey] = $storeCategories; return $storeCategories; } /** * Retrieve category url * * @param ModelCategory $category * @return string */ public function getCategoryUrl($category) { if ($category instanceof ModelCategory) { return $category->getUrl(); } return $this->_categoryFactory->create()->setData($category->getData())->getUrl(); } /** * Check if a category can be shown * * @param ModelCategory|int $category * @return bool */ public function canShow($category) { if (is_int($category)) { try { $category = $this->categoryRepository->get($category); } catch (NoSuchEntityException $e) { return false; } } else { if (!$category->getId()) { return false; } } if (!$category->getIsActive()) { return false; } if (!$category->isInRootCategoryList()) { return false; } return true; } /** * Check if can be used for category * * @param null|string|bool|int|Store $store * @return bool */ public function canUseCanonicalTag($store = null) { return $this->scopeConfig->getValue( self::XML_PATH_USE_CATEGORY_CANONICAL_TAG, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store ); } /** * @inheritDoc */ public function _resetState(): void { $this->_storeCategories = []; } }