_productCollectionFactory = $productCollectionFactory; $this->_catalogLayer = $layerResolver->get(); $this->httpContext = $httpContext; $this->_catalogCategory = $catalogCategory; $this->_registry = $registry; $this->flatState = $flatState; $this->_categoryInstance = $categoryFactory->create(); parent::__construct($context, $data); } /** * @return void */ protected function _construct() { $this->addData( [ 'cache_lifetime' => false, 'cache_tags' => [Category::CACHE_TAG, \Magento\Store\Model\Group::CACHE_TAG], ] ); } /** * Get current category * * @return Category */ public function getCategory() { return $this->_registry->registry('current_category'); } /** * Get Key pieces for caching block content * * @return array */ public function getCacheKeyInfo() { $shortCacheId = [ 'CATALOG_NAVIGATION', $this->_storeManager->getStore()->getId(), $this->_design->getDesignTheme()->getId(), $this->httpContext->getValue(Context::CONTEXT_GROUP), 'template' => $this->getTemplate(), 'name' => $this->getNameInLayout(), $this->getCurrentCategoryKey(), ]; $cacheId = $shortCacheId; $shortCacheId = array_values($shortCacheId); $shortCacheId = implode('|', $shortCacheId); $shortCacheId = md5($shortCacheId); $cacheId['category_path'] = $this->getCurrentCategoryKey(); $cacheId['short_cache_id'] = $shortCacheId; return $cacheId; } /** * Get current category key * * @return string */ public function getCurrentCategoryKey() { if (!$this->_currentCategoryKey) { $category = $this->_registry->registry('current_category'); if ($category) { $this->_currentCategoryKey = $category->getPath(); } else { $this->_currentCategoryKey = $this->_storeManager->getStore()->getRootCategoryId(); } } return $this->_currentCategoryKey; } /** * Retrieve child categories of current category * * @return \Magento\Framework\Data\Tree\Node\Collection */ public function getCurrentChildCategories() { $categories = $this->_catalogLayer->getCurrentCategory()->getChildrenCategories(); /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */ $productCollection = $this->_productCollectionFactory->create(); $this->_catalogLayer->prepareProductCollection($productCollection); $productCollection->addCountToCategories($categories); return $categories; } /** * Check activity of category * * @param \Magento\Framework\DataObject $category * @return bool */ public function isCategoryActive($category) { if ($this->getCurrentCategory()) { return in_array($category->getId(), $this->getCurrentCategory()->getPathIds()); } return false; } /** * Get url for category data * * @param Category $category * @return string */ public function getCategoryUrl($category) { if ($category instanceof Category) { $url = $category->getUrl(); } else { $url = $this->_categoryInstance->setData($category->getData())->getUrl(); } return $url; } /** * Enter description here... * * @return Category */ public function getCurrentCategory() { return $this->_catalogLayer->getCurrentCategory(); } /** * Return identifiers for produced content * * @return array */ public function getIdentities() { return [\Magento\Catalog\Model\Category::CACHE_TAG, \Magento\Store\Model\Group::CACHE_TAG]; } }