Skip to content

Commit daffedf

Browse files
committed
MAGETWO-95818: [Magento Cloud] Default value for category URL path does not save
- Added reseting category url_path attribute if url_key was set to default value
1 parent ec5fcb5 commit daffedf

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-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: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Magento\Framework\Event\ObserverInterface;
1414
use Magento\Store\Model\Store;
1515

16+
/**
17+
* Class observer to initiate generation category url_path
18+
*/
1619
class CategoryUrlPathAutogeneratorObserver implements ObserverInterface
1720
{
1821
/**
@@ -46,6 +49,8 @@ public function __construct(
4649
}
4750

4851
/**
52+
* Generate Category Url Path
53+
*
4954
* @param \Magento\Framework\Event\Observer $observer
5055
* @return void
5156
* @throws \Magento\Framework\Exception\LocalizedException
@@ -57,21 +62,40 @@ public function execute(\Magento\Framework\Event\Observer $observer)
5762
$useDefaultAttribute = !$category->isObjectNew() && !empty($category->getData('use_default')['url_key']);
5863
if ($category->getUrlKey() !== false && !$useDefaultAttribute) {
5964
$resultUrlKey = $this->categoryUrlPathGenerator->getUrlKey($category);
60-
if (empty($resultUrlKey)) {
61-
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid URL key'));
62-
}
63-
$category->setUrlKey($resultUrlKey)
64-
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
65-
if (!$category->isObjectNew()) {
66-
$category->getResource()->saveAttribute($category, 'url_path');
67-
if ($category->dataHasChangedFor('url_path')) {
68-
$this->updateUrlPathForChildren($category);
69-
}
65+
$this->updateUrlKey($category, $resultUrlKey);
66+
} else if ($useDefaultAttribute) {
67+
$resultUrlKey = $category->formatUrlKey($category->getOrigData('name'));
68+
$this->updateUrlKey($category, $resultUrlKey);
69+
$category->setUrlKey(null)->setUrlPath(null);
70+
}
71+
}
72+
73+
/**
74+
* Update Url Key
75+
*
76+
* @param Category $category
77+
* @param string $urlKey
78+
* @throws \Magento\Framework\Exception\LocalizedException
79+
* @throws \Magento\Framework\Exception\NoSuchEntityException
80+
*/
81+
private function updateUrlKey($category, $urlKey)
82+
{
83+
if (empty($urlKey)) {
84+
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid URL key'));
85+
}
86+
$category->setUrlKey($urlKey)
87+
->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
88+
if (!$category->isObjectNew()) {
89+
$category->getResource()->saveAttribute($category, 'url_path');
90+
if ($category->dataHasChangedFor('url_path')) {
91+
$this->updateUrlPathForChildren($category);
7092
}
7193
}
7294
}
7395

7496
/**
97+
* Update URL path for children
98+
*
7599
* @param Category $category
76100
* @return void
77101
*/
@@ -94,8 +118,13 @@ protected function updateUrlPathForChildren(Category $category)
94118
}
95119
} else {
96120
foreach ($children as $child) {
121+
/** @var Category $child */
97122
$child->setStoreId($category->getStoreId());
98-
$this->updateUrlPathForCategory($child);
123+
if ($child->getParentId() === $category->getId()) {
124+
$this->updateUrlPathForCategory($child, $category);
125+
} else {
126+
$this->updateUrlPathForCategory($child);
127+
}
99128
}
100129
}
101130
}
@@ -112,13 +141,17 @@ protected function isGlobalScope($storeId)
112141
}
113142

114143
/**
144+
* Update URL path for category
145+
*
115146
* @param Category $category
147+
* @param Category|null $parentCategory
116148
* @return void
149+
* @throws \Magento\Framework\Exception\NoSuchEntityException
117150
*/
118-
protected function updateUrlPathForCategory(Category $category)
151+
protected function updateUrlPathForCategory(Category $category, Category $parentCategory = null)
119152
{
120153
$category->unsUrlPath();
121-
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
154+
$category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category, $parentCategory));
122155
$category->getResource()->saveAttribute($category, 'url_path');
123156
}
124157
}

0 commit comments

Comments
 (0)