13
13
use Magento \Framework \Event \ObserverInterface ;
14
14
use Magento \Store \Model \Store ;
15
15
16
+ /**
17
+ * Class observer to initiate generation category url_path
18
+ */
16
19
class CategoryUrlPathAutogeneratorObserver implements ObserverInterface
17
20
{
18
21
/**
@@ -46,6 +49,8 @@ public function __construct(
46
49
}
47
50
48
51
/**
52
+ * Generate Category Url Path
53
+ *
49
54
* @param \Magento\Framework\Event\Observer $observer
50
55
* @return void
51
56
* @throws \Magento\Framework\Exception\LocalizedException
@@ -57,21 +62,40 @@ public function execute(\Magento\Framework\Event\Observer $observer)
57
62
$ useDefaultAttribute = !$ category ->isObjectNew () && !empty ($ category ->getData ('use_default ' )['url_key ' ]);
58
63
if ($ category ->getUrlKey () !== false && !$ useDefaultAttribute ) {
59
64
$ 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 );
70
92
}
71
93
}
72
94
}
73
95
74
96
/**
97
+ * Update URL path for children
98
+ *
75
99
* @param Category $category
76
100
* @return void
77
101
*/
@@ -94,8 +118,13 @@ protected function updateUrlPathForChildren(Category $category)
94
118
}
95
119
} else {
96
120
foreach ($ children as $ child ) {
121
+ /** @var Category $child */
97
122
$ 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
+ }
99
128
}
100
129
}
101
130
}
@@ -112,13 +141,17 @@ protected function isGlobalScope($storeId)
112
141
}
113
142
114
143
/**
144
+ * Update URL path for category
145
+ *
115
146
* @param Category $category
147
+ * @param Category|null $parentCategory
116
148
* @return void
149
+ * @throws \Magento\Framework\Exception\NoSuchEntityException
117
150
*/
118
- protected function updateUrlPathForCategory (Category $ category )
151
+ protected function updateUrlPathForCategory (Category $ category, Category $ parentCategory = null )
119
152
{
120
153
$ category ->unsUrlPath ();
121
- $ category ->setUrlPath ($ this ->categoryUrlPathGenerator ->getUrlPath ($ category ));
154
+ $ category ->setUrlPath ($ this ->categoryUrlPathGenerator ->getUrlPath ($ category, $ parentCategory ));
122
155
$ category ->getResource ()->saveAttribute ($ category , 'url_path ' );
123
156
}
124
157
}
0 commit comments