Skip to content

Commit 790540d

Browse files
Merge remote-tracking branch 'remotes/github/MAGETWO-95818' into EPAM-PR-23
2 parents a6eab40 + c841115 commit 790540d

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlPathGenerator.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Magento\Catalog\Api\CategoryRepositoryInterface;
99
use Magento\Catalog\Model\Category;
1010

11+
/**
12+
* Class for generation category url_path
13+
*/
1114
class CategoryUrlPathGenerator
1215
{
1316
/**
@@ -61,9 +64,11 @@ public function __construct(
6164
* Build category URL path
6265
*
6366
* @param \Magento\Catalog\Api\Data\CategoryInterface|\Magento\Framework\Model\AbstractModel $category
67+
* @param null|\Magento\Catalog\Api\Data\CategoryInterface|\Magento\Framework\Model\AbstractModel $parentCategory
6468
* @return string
69+
* @throws \Magento\Framework\Exception\NoSuchEntityException
6570
*/
66-
public function getUrlPath($category)
71+
public function getUrlPath($category, $parentCategory = null)
6772
{
6873
if (in_array($category->getParentId(), [Category::ROOT_CATEGORY_ID, Category::TREE_ROOT_ID])) {
6974
return '';
@@ -77,15 +82,17 @@ public function getUrlPath($category)
7782
return $category->getUrlPath();
7883
}
7984
if ($this->isNeedToGenerateUrlPathForParent($category)) {
80-
$parentPath = $this->getUrlPath(
81-
$this->categoryRepository->get($category->getParentId(), $category->getStoreId())
82-
);
85+
$parentCategory = $parentCategory === null ?
86+
$this->categoryRepository->get($category->getParentId(), $category->getStoreId()) : $parentCategory;
87+
$parentPath = $this->getUrlPath($parentCategory);
8388
$path = $parentPath === '' ? $path : $parentPath . '/' . $path;
8489
}
8590
return $path;
8691
}
8792

8893
/**
94+
* Define whether we should generate URL path for parent
95+
*
8996
* @param \Magento\Catalog\Model\Category $category
9097
* @return bool
9198
*/

app/code/Magento/CatalogUrlRewrite/Observer/CategoryUrlPathAutogeneratorObserver.php

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,33 @@ public function execute(\Magento\Framework\Event\Observer $observer)
7171
$useDefaultAttribute = !$category->isObjectNew() && !empty($category->getData('use_default')['url_key']);
7272
if ($category->getUrlKey() !== false && !$useDefaultAttribute) {
7373
$resultUrlKey = $this->categoryUrlPathGenerator->getUrlKey($category);
74-
if (empty($resultUrlKey)) {
75-
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid URL key'));
76-
}
77-
$category->setUrlKey($resultUrlKey)
78-
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
79-
if (!$category->isObjectNew()) {
80-
$category->getResource()->saveAttribute($category, 'url_path');
81-
if ($category->dataHasChangedFor('url_path')) {
82-
$this->updateUrlPathForChildren($category);
83-
}
74+
$this->updateUrlKey($category, $resultUrlKey);
75+
} else if ($useDefaultAttribute) {
76+
$resultUrlKey = $category->formatUrlKey($category->getOrigData('name'));
77+
$this->updateUrlKey($category, $resultUrlKey);
78+
$category->setUrlKey(null)->setUrlPath(null);
79+
}
80+
}
81+
82+
/**
83+
* Update Url Key
84+
*
85+
* @param Category $category
86+
* @param string $urlKey
87+
* @throws \Magento\Framework\Exception\LocalizedException
88+
* @throws \Magento\Framework\Exception\NoSuchEntityException
89+
*/
90+
private function updateUrlKey($category, $urlKey)
91+
{
92+
if (empty($urlKey)) {
93+
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid URL key'));
94+
}
95+
$category->setUrlKey($urlKey)
96+
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
97+
if (!$category->isObjectNew()) {
98+
$category->getResource()->saveAttribute($category, 'url_path');
99+
if ($category->dataHasChangedFor('url_path')) {
100+
$this->updateUrlPathForChildren($category);
84101
}
85102
}
86103
}
@@ -110,8 +127,13 @@ protected function updateUrlPathForChildren(Category $category)
110127
} else {
111128
$children = $this->childrenCategoriesProvider->getChildren($category, true);
112129
foreach ($children as $child) {
130+
/** @var Category $child */
113131
$child->setStoreId($category->getStoreId());
114-
$this->updateUrlPathForCategory($child);
132+
if ($child->getParentId() === $category->getId()) {
133+
$this->updateUrlPathForCategory($child, $category);
134+
} else {
135+
$this->updateUrlPathForCategory($child);
136+
}
115137
}
116138
}
117139
}
@@ -131,12 +153,14 @@ protected function isGlobalScope($storeId)
131153
* Update url path for category.
132154
*
133155
* @param Category $category
156+
* @param Category|null $parentCategory
134157
* @return void
158+
* @throws \Magento\Framework\Exception\NoSuchEntityException
135159
*/
136-
protected function updateUrlPathForCategory(Category $category)
160+
protected function updateUrlPathForCategory(Category $category, Category $parentCategory = null)
137161
{
138162
$category->unsUrlPath();
139-
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
163+
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category, $parentCategory));
140164
$category->getResource()->saveAttribute($category, 'url_path');
141165
}
142166
}

0 commit comments

Comments
 (0)