@@ -152,11 +152,56 @@ public function getUrl($identifier, $controllerName)
152
152
'_direct ' => $ this ->getUrlPath ($ identifier , $ controllerName )
153
153
]);
154
154
155
- $ url = trim ($ url , '/ ' );
155
+ return $ url ;
156
+ }
157
+
158
+ /**
159
+ * Retrieve canonical url
160
+ * @param \Magento\Framework\Model\AbstractModel $object
161
+ * @return string
162
+ */
163
+ public function getCanonicalUrl (\Magento \Framework \Model \AbstractModel $ object )
164
+ {
165
+ $ storeIds = $ object ->getStoreIds ();
166
+ $ useDefaultStore = false ;
167
+ $ currentStore = $ this ->_storeManager ->getStore ($ object ->getStoreId ());
168
+
169
+ if (is_array ($ storeIds )) {
170
+ if (0 == array_values ($ storeIds )[0 ]) {
171
+ $ useDefaultStore = true ;
172
+ } elseif (count ($ storeIds > 1 )) {
173
+ foreach ($ storeIds as $ storeId ) {
174
+ if ($ storeId != $ currentStore ->getId ()) {
175
+ $ store = $ this ->_storeManager ->getStore ($ storeId );
176
+ if ($ store ->getGroupId () == $ currentStore ->getGroupId ()) {
177
+ $ useDefaultStore = true ;
178
+ break ;
179
+ }
180
+ }
181
+ }
182
+ }
183
+ }
184
+
185
+ $ storeChanged = false ;
186
+ if ($ useDefaultStore ) {
187
+ $ newStore = $ currentStore ->getGroup ()->getDefaultStore ();
188
+ $ origStore = $ this ->_url ->getScope ();
189
+ if ($ newStore ->getId () != $ origStore ->getId ()) {
190
+ $ this ->_url ->setScope ($ newStore );
191
+ $ storeChanged = true ;
192
+ }
193
+ }
194
+
195
+ $ url = $ this ->getUrl ($ object ->getIdentifier (), $ object ->getControllerName ());
196
+
197
+ if ($ storeChanged ) {
198
+ $ this ->_url ->setScope ($ origStore );
199
+ }
156
200
157
201
return $ url ;
158
202
}
159
203
204
+
160
205
/**
161
206
* Retrieve blog url path
162
207
* @param string $identifier
@@ -168,16 +213,16 @@ public function getUrlPath($identifier, $controllerName)
168
213
$ identifier = $ this ->getExpandedItentifier ($ identifier );
169
214
switch ($ this ->getPermalinkType ()) {
170
215
case self ::PERMALINK_TYPE_DEFAULT :
171
- $ path = $ this ->getRoute () . '/ ' . $ this ->getRoute ($ controllerName ) . '/ ' . $ identifier ;
216
+ $ path = $ this ->getRoute () . '/ ' . $ this ->getRoute ($ controllerName ) . '/ ' . $ identifier . ( $ identifier ? ' / ' : '' ) ;
172
217
break ;
173
218
case self ::PERMALINK_TYPE_SHORT :
174
219
if ($ controllerName == self ::CONTROLLER_SEARCH
175
220
|| $ controllerName == self ::CONTROLLER_AUTHOR
176
221
|| $ controllerName == self ::CONTROLLER_TAG
177
222
) {
178
- $ path = $ this ->getRoute () . '/ ' . $ this ->getRoute ($ controllerName ) . '/ ' . $ identifier ;
223
+ $ path = $ this ->getRoute () . '/ ' . $ this ->getRoute ($ controllerName ) . '/ ' . $ identifier . ( $ identifier ? ' / ' : '' ) ;
179
224
} else {
180
- $ path = $ this ->getRoute () . '/ ' . $ identifier ;
225
+ $ path = $ this ->getRoute () . '/ ' . $ identifier . ( $ identifier ? ' / ' : '' ) ;
181
226
}
182
227
break ;
183
228
}
@@ -297,4 +342,5 @@ protected function _getConfig($key)
297
342
);
298
343
}
299
344
345
+
300
346
}
0 commit comments