Navigation.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Block;
  7. use Magento\Catalog\Model\Category;
  8. use Magento\Customer\Model\Context;
  9. /**
  10. * Catalog navigation
  11. *
  12. * @api
  13. * @SuppressWarnings(PHPMD.LongVariable)
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. * @since 100.0.2
  16. */
  17. class Navigation extends \Magento\Framework\View\Element\Template implements
  18. \Magento\Framework\DataObject\IdentityInterface
  19. {
  20. /**
  21. * @var Category
  22. */
  23. protected $_categoryInstance;
  24. /**
  25. * Current category key
  26. *
  27. * @var string
  28. */
  29. protected $_currentCategoryKey;
  30. /**
  31. * Array of level position counters
  32. *
  33. * @var array
  34. */
  35. protected $_itemLevelPositions = [];
  36. /**
  37. * Catalog category
  38. *
  39. * @var \Magento\Catalog\Helper\Category
  40. */
  41. protected $_catalogCategory;
  42. /**
  43. * @var \Magento\Framework\Registry
  44. */
  45. protected $_registry;
  46. /**
  47. * Customer session
  48. *
  49. * @var \Magento\Framework\App\Http\Context
  50. */
  51. protected $httpContext;
  52. /**
  53. * Catalog layer
  54. *
  55. * @var \Magento\Catalog\Model\Layer
  56. */
  57. protected $_catalogLayer;
  58. /**
  59. * Product collection factory
  60. *
  61. * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
  62. */
  63. protected $_productCollectionFactory;
  64. /**
  65. * @var \Magento\Catalog\Model\Indexer\Category\Flat\State
  66. */
  67. protected $flatState;
  68. /**
  69. * @param \Magento\Framework\View\Element\Template\Context $context
  70. * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
  71. * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
  72. * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
  73. * @param \Magento\Framework\App\Http\Context $httpContext
  74. * @param \Magento\Catalog\Helper\Category $catalogCategory
  75. * @param \Magento\Framework\Registry $registry
  76. * @param \Magento\Catalog\Model\Indexer\Category\Flat\State $flatState
  77. * @param array $data
  78. */
  79. public function __construct(
  80. \Magento\Framework\View\Element\Template\Context $context,
  81. \Magento\Catalog\Model\CategoryFactory $categoryFactory,
  82. \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
  83. \Magento\Catalog\Model\Layer\Resolver $layerResolver,
  84. \Magento\Framework\App\Http\Context $httpContext,
  85. \Magento\Catalog\Helper\Category $catalogCategory,
  86. \Magento\Framework\Registry $registry,
  87. \Magento\Catalog\Model\Indexer\Category\Flat\State $flatState,
  88. array $data = []
  89. ) {
  90. $this->_productCollectionFactory = $productCollectionFactory;
  91. $this->_catalogLayer = $layerResolver->get();
  92. $this->httpContext = $httpContext;
  93. $this->_catalogCategory = $catalogCategory;
  94. $this->_registry = $registry;
  95. $this->flatState = $flatState;
  96. $this->_categoryInstance = $categoryFactory->create();
  97. parent::__construct($context, $data);
  98. }
  99. /**
  100. * @return void
  101. */
  102. protected function _construct()
  103. {
  104. $this->addData(
  105. [
  106. 'cache_lifetime' => false,
  107. 'cache_tags' => [Category::CACHE_TAG, \Magento\Store\Model\Group::CACHE_TAG],
  108. ]
  109. );
  110. }
  111. /**
  112. * Get current category
  113. *
  114. * @return Category
  115. */
  116. public function getCategory()
  117. {
  118. return $this->_registry->registry('current_category');
  119. }
  120. /**
  121. * Get Key pieces for caching block content
  122. *
  123. * @return array
  124. */
  125. public function getCacheKeyInfo()
  126. {
  127. $shortCacheId = [
  128. 'CATALOG_NAVIGATION',
  129. $this->_storeManager->getStore()->getId(),
  130. $this->_design->getDesignTheme()->getId(),
  131. $this->httpContext->getValue(Context::CONTEXT_GROUP),
  132. 'template' => $this->getTemplate(),
  133. 'name' => $this->getNameInLayout(),
  134. $this->getCurrentCategoryKey(),
  135. ];
  136. $cacheId = $shortCacheId;
  137. $shortCacheId = array_values($shortCacheId);
  138. $shortCacheId = implode('|', $shortCacheId);
  139. $shortCacheId = md5($shortCacheId);
  140. $cacheId['category_path'] = $this->getCurrentCategoryKey();
  141. $cacheId['short_cache_id'] = $shortCacheId;
  142. return $cacheId;
  143. }
  144. /**
  145. * Get current category key
  146. *
  147. * @return string
  148. */
  149. public function getCurrentCategoryKey()
  150. {
  151. if (!$this->_currentCategoryKey) {
  152. $category = $this->_registry->registry('current_category');
  153. if ($category) {
  154. $this->_currentCategoryKey = $category->getPath();
  155. } else {
  156. $this->_currentCategoryKey = $this->_storeManager->getStore()->getRootCategoryId();
  157. }
  158. }
  159. return $this->_currentCategoryKey;
  160. }
  161. /**
  162. * Retrieve child categories of current category
  163. *
  164. * @return \Magento\Framework\Data\Tree\Node\Collection
  165. */
  166. public function getCurrentChildCategories()
  167. {
  168. $categories = $this->_catalogLayer->getCurrentCategory()->getChildrenCategories();
  169. /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection */
  170. $productCollection = $this->_productCollectionFactory->create();
  171. $this->_catalogLayer->prepareProductCollection($productCollection);
  172. $productCollection->addCountToCategories($categories);
  173. return $categories;
  174. }
  175. /**
  176. * Check activity of category
  177. *
  178. * @param \Magento\Framework\DataObject $category
  179. * @return bool
  180. */
  181. public function isCategoryActive($category)
  182. {
  183. if ($this->getCurrentCategory()) {
  184. return in_array($category->getId(), $this->getCurrentCategory()->getPathIds());
  185. }
  186. return false;
  187. }
  188. /**
  189. * Get url for category data
  190. *
  191. * @param Category $category
  192. * @return string
  193. */
  194. public function getCategoryUrl($category)
  195. {
  196. if ($category instanceof Category) {
  197. $url = $category->getUrl();
  198. } else {
  199. $url = $this->_categoryInstance->setData($category->getData())->getUrl();
  200. }
  201. return $url;
  202. }
  203. /**
  204. * Enter description here...
  205. *
  206. * @return Category
  207. */
  208. public function getCurrentCategory()
  209. {
  210. return $this->_catalogLayer->getCurrentCategory();
  211. }
  212. /**
  213. * Return identifiers for produced content
  214. *
  215. * @return array
  216. */
  217. public function getIdentities()
  218. {
  219. return [\Magento\Catalog\Model\Category::CACHE_TAG, \Magento\Store\Model\Group::CACHE_TAG];
  220. }
  221. }