Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Category.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Catalog\Helper;
7 
13 
19 class Category extends AbstractHelper
20 {
21  const XML_PATH_USE_CATEGORY_CANONICAL_TAG = 'catalog/seo/category_canonical_tag';
22 
23  const XML_PATH_CATEGORY_ROOT_ID = 'catalog/category/root_id';
24 
30  protected $_storeCategories = [];
31 
37  protected $_storeManager;
38 
44  protected $_categoryFactory;
45 
52 
57 
65  public function __construct(
66  \Magento\Framework\App\Helper\Context $context,
67  \Magento\Catalog\Model\CategoryFactory $categoryFactory,
68  \Magento\Store\Model\StoreManagerInterface $storeManager,
69  \Magento\Framework\Data\CollectionFactory $dataCollectionFactory,
71  ) {
72  $this->_categoryFactory = $categoryFactory;
73  $this->_storeManager = $storeManager;
74  $this->_dataCollectionFactory = $dataCollectionFactory;
75  $this->categoryRepository = $categoryRepository;
76  parent::__construct($context);
77  }
78 
88  public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
89  {
90  $parent = $this->_storeManager->getStore()->getRootCategoryId();
91  $cacheKey = sprintf('%d-%d-%d-%d', $parent, $sorted, $asCollection, $toLoad);
92  if (isset($this->_storeCategories[$cacheKey])) {
93  return $this->_storeCategories[$cacheKey];
94  }
95 
99  $category = $this->_categoryFactory->create();
100  /* @var $category ModelCategory */
101  if (!$category->checkId($parent)) {
102  if ($asCollection) {
103  return $this->_dataCollectionFactory->create();
104  }
105  return [];
106  }
107 
108  $recursionLevel = max(
109  0,
110  (int)$this->scopeConfig->getValue(
111  'catalog/navigation/max_depth',
113  )
114  );
115  $storeCategories = $category->getCategories($parent, $recursionLevel, $sorted, $asCollection, $toLoad);
116 
117  $this->_storeCategories[$cacheKey] = $storeCategories;
118  return $storeCategories;
119  }
120 
127  public function getCategoryUrl($category)
128  {
129  if ($category instanceof ModelCategory) {
130  return $category->getUrl();
131  }
132  return $this->_categoryFactory->create()->setData($category->getData())->getUrl();
133  }
134 
141  public function canShow($category)
142  {
143  if (is_int($category)) {
144  try {
145  $category = $this->categoryRepository->get($category);
146  } catch (NoSuchEntityException $e) {
147  return false;
148  }
149  } else {
150  if (!$category->getId()) {
151  return false;
152  }
153  }
154 
155  if (!$category->getIsActive()) {
156  return false;
157  }
158  if (!$category->isInRootCategoryList()) {
159  return false;
160  }
161 
162  return true;
163  }
164 
171  public function canUseCanonicalTag($store = null)
172  {
173  return $this->scopeConfig->getValue(
174  self::XML_PATH_USE_CATEGORY_CANONICAL_TAG,
176  $store
177  );
178  }
179 }
$storeManager
__construct(\Magento\Framework\App\Helper\Context $context, \Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Data\CollectionFactory $dataCollectionFactory, CategoryRepositoryInterface $categoryRepository)
Definition: Category.php:65
getStoreCategories($sorted=false, $asCollection=false, $toLoad=true)
Definition: Category.php:88