Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractCategory.php
Go to the documentation of this file.
1 <?php
7 
10 
15 {
21  protected $_coreRegistry = null;
22 
26  protected $_categoryTree;
27 
31  protected $_categoryFactory;
32 
36  protected $_withProductCount;
37 
45  public function __construct(
46  \Magento\Backend\Block\Template\Context $context,
47  \Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree,
48  \Magento\Framework\Registry $registry,
49  \Magento\Catalog\Model\CategoryFactory $categoryFactory,
50  array $data = []
51  ) {
52  $this->_categoryTree = $categoryTree;
53  $this->_coreRegistry = $registry;
54  $this->_categoryFactory = $categoryFactory;
55  $this->_withProductCount = true;
56  parent::__construct($context, $data);
57  }
58 
64  public function getCategory()
65  {
66  return $this->_coreRegistry->registry('category');
67  }
68 
72  public function getCategoryId()
73  {
74  if ($this->getCategory()) {
75  return $this->getCategory()->getId();
76  }
77  return \Magento\Catalog\Model\Category::TREE_ROOT_ID;
78  }
79 
83  public function getCategoryName()
84  {
85  return $this->getCategory()->getName();
86  }
87 
91  public function getCategoryPath()
92  {
93  if ($this->getCategory()) {
94  return $this->getCategory()->getPath();
95  }
96  return \Magento\Catalog\Model\Category::TREE_ROOT_ID;
97  }
98 
102  public function hasStoreRootCategory()
103  {
104  $root = $this->getRoot();
105  if ($root && $root->getId()) {
106  return true;
107  }
108  return false;
109  }
110 
114  public function getStore()
115  {
116  $storeId = (int)$this->getRequest()->getParam('store');
117  return $this->_storeManager->getStore($storeId);
118  }
119 
126  public function getRoot($parentNodeCategory = null, $recursionLevel = 3)
127  {
128  if ($parentNodeCategory !== null && $parentNodeCategory->getId()) {
129  return $this->getNode($parentNodeCategory, $recursionLevel);
130  }
131  $root = $this->_coreRegistry->registry('root');
132  if ($root === null) {
133  $storeId = (int)$this->getRequest()->getParam('store');
134 
135  if ($storeId) {
136  $store = $this->_storeManager->getStore($storeId);
137  $rootId = $store->getRootCategoryId();
138  } else {
140  }
141 
142  $tree = $this->_categoryTree->load(null, $recursionLevel);
143 
144  if ($this->getCategory()) {
145  $tree->loadEnsuredNodes($this->getCategory(), $tree->getNodeById($rootId));
146  }
147 
148  $tree->addCollectionData($this->getCategoryCollection());
149 
150  $root = $tree->getNodeById($rootId);
151 
152  if ($root && $rootId != \Magento\Catalog\Model\Category::TREE_ROOT_ID) {
153  $root->setIsVisible(true);
154  } elseif ($root && $root->getId() == \Magento\Catalog\Model\Category::TREE_ROOT_ID) {
155  $root->setName(__('Root'));
156  }
157 
158  $this->_coreRegistry->register('root', $root);
159  }
160 
161  return $root;
162  }
163 
167  protected function _getDefaultStoreId()
168  {
169  return \Magento\Store\Model\Store::DEFAULT_STORE_ID;
170  }
171 
175  public function getCategoryCollection()
176  {
177  $storeId = $this->getRequest()->getParam('store', $this->_getDefaultStoreId());
178  $collection = $this->getData('category_collection');
179  if ($collection === null) {
180  $collection = $this->_categoryFactory->create()->getCollection();
181 
182  $collection->addAttributeToSelect(
183  'name'
184  )->addAttributeToSelect(
185  'is_active'
186  )->setProductStoreId(
187  $storeId
188  )->setLoadProductCount(
189  $this->_withProductCount
190  )->setStoreId(
191  $storeId
192  );
193 
194  $this->setData('category_collection', $collection);
195  }
196  return $collection;
197  }
198 
209  public function getRootByIds($ids)
210  {
211  $root = $this->_coreRegistry->registry('root');
212  if (null === $root) {
213  $ids = $this->_categoryTree->getExistingCategoryIdsBySpecifiedIds($ids);
214  $tree = $this->_categoryTree->loadByIds($ids);
216  $root = $tree->getNodeById($rootId);
217  if ($root && $rootId != \Magento\Catalog\Model\Category::TREE_ROOT_ID) {
218  $root->setIsVisible(true);
219  } elseif ($root && $root->getId() == \Magento\Catalog\Model\Category::TREE_ROOT_ID) {
220  $root->setName(__('Root'));
221  }
222 
223  $tree->addCollectionData($this->getCategoryCollection());
224  $this->_coreRegistry->register('root', $root);
225  }
226  return $root;
227  }
228 
234  public function getNode($parentNodeCategory, $recursionLevel = 2)
235  {
236  $nodeId = $parentNodeCategory->getId();
237  $node = $this->_categoryTree->loadNode($nodeId);
238  $node->loadChildren($recursionLevel);
239 
240  if ($node && $nodeId != \Magento\Catalog\Model\Category::TREE_ROOT_ID) {
241  $node->setIsVisible(true);
242  } elseif ($node && $node->getId() == \Magento\Catalog\Model\Category::TREE_ROOT_ID) {
243  $node->setName(__('Root'));
244  }
245 
246  $this->_categoryTree->addCollectionData($this->getCategoryCollection());
247 
248  return $node;
249  }
250 
255  public function getSaveUrl(array $args = [])
256  {
257  $params = ['_current' => false, '_query' => false, 'store' => $this->getStore()->getId()];
258  $params = array_merge($params, $args);
259  return $this->getUrl('catalog/*/save', $params);
260  }
261 
265  public function getEditUrl()
266  {
267  return $this->getUrl(
268  'catalog/category/edit',
269  ['store' => null, '_query' => false, 'id' => null, 'parent' => null]
270  );
271  }
272 
278  public function getRootIds()
279  {
280  $ids = $this->getData('root_ids');
281  if ($ids === null) {
283  foreach ($this->_storeManager->getGroups() as $store) {
284  $ids[] = $store->getRootCategoryId();
285  }
286  $this->setData('root_ids', $ids);
287  }
288  return $ids;
289  }
290 }
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Catalog\Model\ResourceModel\Category\Tree $categoryTree, \Magento\Framework\Registry $registry, \Magento\Catalog\Model\CategoryFactory $categoryFactory, array $data=[])
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
getRoot($parentNodeCategory=null, $recursionLevel=3)
setData($key, $value=null)
Definition: DataObject.php:72
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18