@@ -152,11 +152,56 @@ public function getUrl($identifier, $controllerName)
152152 '_direct ' => $ this ->getUrlPath ($ identifier , $ controllerName )
153153 ]);
154154
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+ }
156200
157201 return $ url ;
158202 }
159203
204+
160205 /**
161206 * Retrieve blog url path
162207 * @param string $identifier
@@ -168,16 +213,16 @@ public function getUrlPath($identifier, $controllerName)
168213 $ identifier = $ this ->getExpandedItentifier ($ identifier );
169214 switch ($ this ->getPermalinkType ()) {
170215 case self ::PERMALINK_TYPE_DEFAULT :
171- $ path = $ this ->getRoute () . '/ ' . $ this ->getRoute ($ controllerName ) . '/ ' . $ identifier ;
216+ $ path = $ this ->getRoute () . '/ ' . $ this ->getRoute ($ controllerName ) . '/ ' . $ identifier . ( $ identifier ? ' / ' : '' ) ;
172217 break ;
173218 case self ::PERMALINK_TYPE_SHORT :
174219 if ($ controllerName == self ::CONTROLLER_SEARCH
175220 || $ controllerName == self ::CONTROLLER_AUTHOR
176221 || $ controllerName == self ::CONTROLLER_TAG
177222 ) {
178- $ path = $ this ->getRoute () . '/ ' . $ this ->getRoute ($ controllerName ) . '/ ' . $ identifier ;
223+ $ path = $ this ->getRoute () . '/ ' . $ this ->getRoute ($ controllerName ) . '/ ' . $ identifier . ( $ identifier ? ' / ' : '' ) ;
179224 } else {
180- $ path = $ this ->getRoute () . '/ ' . $ identifier ;
225+ $ path = $ this ->getRoute () . '/ ' . $ identifier . ( $ identifier ? ' / ' : '' ) ;
181226 }
182227 break ;
183228 }
@@ -297,4 +342,5 @@ protected function _getConfig($key)
297342 );
298343 }
299344
345+
300346}
0 commit comments