Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Attributes
Category Class Reference
Inheritance diagram for Category:
AbstractHelper

Public Member Functions

 __construct (\Magento\Framework\App\Helper\Context $context, \Magento\Catalog\Model\CategoryFactory $categoryFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Data\CollectionFactory $dataCollectionFactory, CategoryRepositoryInterface $categoryRepository)
 
 getStoreCategories ($sorted=false, $asCollection=false, $toLoad=true)
 
 getCategoryUrl ($category)
 
 canShow ($category)
 
 canUseCanonicalTag ($store=null)
 
- Public Member Functions inherited from AbstractHelper
 __construct (Context $context)
 
 isModuleOutputEnabled ($moduleName=null)
 

Data Fields

const XML_PATH_USE_CATEGORY_CANONICAL_TAG = 'catalog/seo/category_canonical_tag'
 
const XML_PATH_CATEGORY_ROOT_ID = 'catalog/category/root_id'
 

Protected Attributes

 $_storeCategories = []
 
 $_storeManager
 
 $_categoryFactory
 
 $_dataCollectionFactory
 
 $categoryRepository
 
- Protected Attributes inherited from AbstractHelper
 $_moduleName
 
 $_request
 
 $_moduleManager
 
 $_logger
 
 $_urlBuilder
 
 $_httpHeader
 
 $_eventManager
 
 $_remoteAddress
 
 $urlEncoder
 
 $urlDecoder
 
 $scopeConfig
 
 $_cacheConfig
 

Additional Inherited Members

- Protected Member Functions inherited from AbstractHelper
 _getRequest ()
 
 _getModuleName ()
 
 _getUrl ($route, $params=[])
 

Detailed Description

Catalog category helper

@SuppressWarnings(PHPMD.LongVariable)

Definition at line 19 of file Category.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\App\Helper\Context  $context,
\Magento\Catalog\Model\CategoryFactory  $categoryFactory,
\Magento\Store\Model\StoreManagerInterface  $storeManager,
\Magento\Framework\Data\CollectionFactory  $dataCollectionFactory,
CategoryRepositoryInterface  $categoryRepository 
)
Parameters
\Magento\Framework\App\Helper\Context$context
\Magento\Catalog\Model\CategoryFactory$categoryFactory
\Magento\Store\Model\StoreManagerInterface$storeManager
\Magento\Framework\Data\CollectionFactory$dataCollectionFactory
CategoryRepositoryInterface$categoryRepository

Definition at line 65 of file Category.php.

71  {
72  $this->_categoryFactory = $categoryFactory;
73  $this->_storeManager = $storeManager;
74  $this->_dataCollectionFactory = $dataCollectionFactory;
75  $this->categoryRepository = $categoryRepository;
76  parent::__construct($context);
77  }
$storeManager

Member Function Documentation

◆ canShow()

canShow (   $category)

Check if a category can be shown

Parameters
ModelCategory | int$category
Returns
bool

Definition at line 141 of file Category.php.

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  }

◆ canUseCanonicalTag()

canUseCanonicalTag (   $store = null)

Check if <link rel="canonical"> can be used for category

Parameters
null | string | bool | int | Store$store
Returns
bool

Definition at line 171 of file Category.php.

172  {
173  return $this->scopeConfig->getValue(
174  self::XML_PATH_USE_CATEGORY_CANONICAL_TAG,
176  $store
177  );
178  }

◆ getCategoryUrl()

getCategoryUrl (   $category)

Retrieve category url

Parameters
ModelCategory$category
Returns
string

Definition at line 127 of file Category.php.

128  {
129  if ($category instanceof ModelCategory) {
130  return $category->getUrl();
131  }
132  return $this->_categoryFactory->create()->setData($category->getData())->getUrl();
133  }

◆ getStoreCategories()

getStoreCategories (   $sorted = false,
  $asCollection = false,
  $toLoad = true 
)

Retrieve current store categories

Parameters
bool | string$sorted
bool$asCollection
bool$toLoad
Returns
\Magento\Framework\Data\Tree\Node\Collection or \Magento\Catalog\Model\ResourceModel\Category\Collection or array

Check if parent node of the store still exists

Definition at line 88 of file Category.php.

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  }

Field Documentation

◆ $_categoryFactory

$_categoryFactory
protected

Definition at line 44 of file Category.php.

◆ $_dataCollectionFactory

$_dataCollectionFactory
protected

Definition at line 51 of file Category.php.

◆ $_storeCategories

$_storeCategories = []
protected

Definition at line 30 of file Category.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 37 of file Category.php.

◆ $categoryRepository

$categoryRepository
protected

Definition at line 56 of file Category.php.

◆ XML_PATH_CATEGORY_ROOT_ID

const XML_PATH_CATEGORY_ROOT_ID = 'catalog/category/root_id'

Definition at line 23 of file Category.php.

◆ XML_PATH_USE_CATEGORY_CANONICAL_TAG

const XML_PATH_USE_CATEGORY_CANONICAL_TAG = 'catalog/seo/category_canonical_tag'

Definition at line 21 of file Category.php.


The documentation for this class was generated from the following file: