Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryUrlPathGenerator.php
Go to the documentation of this file.
1 <?php
7 
10 
12 {
17 
21  const XML_PATH_CATEGORY_URL_SUFFIX = 'catalog/seo/category_url_suffix';
22 
28  protected $categoryUrlSuffix = [];
29 
33  protected $scopeConfig;
34 
38  protected $storeManager;
39 
44 
50  public function __construct(
54  ) {
55  $this->storeManager = $storeManager;
56  $this->scopeConfig = $scopeConfig;
57  $this->categoryRepository = $categoryRepository;
58  }
59 
66  public function getUrlPath($category)
67  {
68  if (in_array($category->getParentId(), [Category::ROOT_CATEGORY_ID, Category::TREE_ROOT_ID])) {
69  return '';
70  }
71  $path = $category->getUrlPath();
72  if ($path !== null && !$category->dataHasChangedFor('url_key') && !$category->dataHasChangedFor('parent_id')) {
73  return $path;
74  }
75  $path = $category->getUrlKey();
76  if ($path === false) {
77  return $category->getUrlPath();
78  }
80  $parentPath = $this->getUrlPath(
81  $this->categoryRepository->get($category->getParentId(), $category->getStoreId())
82  );
83  $path = $parentPath === '' ? $path : $parentPath . '/' . $path;
84  }
85  return $path;
86  }
87 
93  {
94  return $category->isObjectNew() || $category->getLevel() >= self::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING;
95  }
96 
104  public function getUrlPathWithSuffix($category, $storeId = null)
105  {
106  if ($storeId === null) {
107  $storeId = $category->getStoreId();
108  }
109  return $this->getUrlPath($category) . $this->getCategoryUrlSuffix($storeId);
110  }
111 
118  protected function getCategoryUrlSuffix($storeId = null)
119  {
120  if ($storeId === null) {
121  $storeId = $this->storeManager->getStore()->getId();
122  }
123  if (!isset($this->categoryUrlSuffix[$storeId])) {
124  $this->categoryUrlSuffix[$storeId] = $this->scopeConfig->getValue(
125  self::XML_PATH_CATEGORY_URL_SUFFIX,
127  $storeId
128  );
129  }
130  return $this->categoryUrlSuffix[$storeId];
131  }
132 
140  {
141  return 'catalog/category/view/id/' . $category->getId();
142  }
143 
150  public function getUrlKey($category)
151  {
152  $urlKey = $category->getUrlKey();
153  return $category->formatUrlKey($urlKey === '' || $urlKey === null ? $category->getName() : $urlKey);
154  }
155 }
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, CategoryRepositoryInterface $categoryRepository)