Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Navigation.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Catalog\Block;
8 
11 
21  \Magento\Framework\DataObject\IdentityInterface
22 {
26  protected $_categoryInstance;
27 
34 
40  protected $_itemLevelPositions = [];
41 
47  protected $_catalogCategory;
48 
52  protected $_registry;
53 
59  protected $httpContext;
60 
66  protected $_catalogLayer;
67 
74 
78  protected $flatState;
79 
91  public function __construct(
92  \Magento\Framework\View\Element\Template\Context $context,
93  \Magento\Catalog\Model\CategoryFactory $categoryFactory,
94  \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
95  \Magento\Catalog\Model\Layer\Resolver $layerResolver,
96  \Magento\Framework\App\Http\Context $httpContext,
97  \Magento\Catalog\Helper\Category $catalogCategory,
98  \Magento\Framework\Registry $registry,
99  \Magento\Catalog\Model\Indexer\Category\Flat\State $flatState,
100  array $data = []
101  ) {
102  $this->_productCollectionFactory = $productCollectionFactory;
103  $this->_catalogLayer = $layerResolver->get();
104  $this->httpContext = $httpContext;
105  $this->_catalogCategory = $catalogCategory;
106  $this->_registry = $registry;
107  $this->flatState = $flatState;
108  $this->_categoryInstance = $categoryFactory->create();
109  parent::__construct($context, $data);
110  }
111 
115  protected function _construct()
116  {
117  $this->addData(
118  [
119  'cache_lifetime' => false,
120  'cache_tags' => [Category::CACHE_TAG, \Magento\Store\Model\Group::CACHE_TAG],
121  ]
122  );
123  }
124 
130  public function getCategory()
131  {
132  return $this->_registry->registry('current_category');
133  }
134 
140  public function getCacheKeyInfo()
141  {
142  $shortCacheId = [
143  'CATALOG_NAVIGATION',
144  $this->_storeManager->getStore()->getId(),
145  $this->_design->getDesignTheme()->getId(),
146  $this->httpContext->getValue(Context::CONTEXT_GROUP),
147  'template' => $this->getTemplate(),
148  'name' => $this->getNameInLayout(),
149  $this->getCurrentCategoryKey(),
150  ];
151  $cacheId = $shortCacheId;
152 
153  $shortCacheId = array_values($shortCacheId);
154  $shortCacheId = implode('|', $shortCacheId);
155  $shortCacheId = md5($shortCacheId);
156 
157  $cacheId['category_path'] = $this->getCurrentCategoryKey();
158  $cacheId['short_cache_id'] = $shortCacheId;
159 
160  return $cacheId;
161  }
162 
168  public function getCurrentCategoryKey()
169  {
170  if (!$this->_currentCategoryKey) {
171  $category = $this->_registry->registry('current_category');
172  if ($category) {
173  $this->_currentCategoryKey = $category->getPath();
174  } else {
175  $this->_currentCategoryKey = $this->_storeManager->getStore()->getRootCategoryId();
176  }
177  }
178 
180  }
181 
187  public function getCurrentChildCategories()
188  {
189  $categories = $this->_catalogLayer->getCurrentCategory()->getChildrenCategories();
191  $productCollection = $this->_productCollectionFactory->create();
192  $this->_catalogLayer->prepareProductCollection($productCollection);
193  $productCollection->addCountToCategories($categories);
194  return $categories;
195  }
196 
203  public function isCategoryActive($category)
204  {
205  if ($this->getCurrentCategory()) {
206  return in_array($category->getId(), $this->getCurrentCategory()->getPathIds());
207  }
208  return false;
209  }
210 
217  public function getCategoryUrl($category)
218  {
219  if ($category instanceof Category) {
220  $url = $category->getUrl();
221  } else {
222  $url = $this->_categoryInstance->setData($category->getData())->getUrl();
223  }
224 
225  return $url;
226  }
227 
233  public function getCurrentCategory()
234  {
235  return $this->_catalogLayer->getCurrentCategory();
236  }
237 
243  public function getIdentities()
244  {
246  }
247 }
$categories
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, \Magento\Catalog\Model\Layer\Resolver $layerResolver, \Magento\Framework\App\Http\Context $httpContext, \Magento\Catalog\Helper\Category $catalogCategory, \Magento\Framework\Registry $registry, \Magento\Catalog\Model\Indexer\Category\Flat\State $flatState, array $data=[])
Definition: Navigation.php:91