From d41bcf0f090322d7ff15becd0ca3dc4ab6fa414c Mon Sep 17 00:00:00 2001 From: Lindsey DiLoreto Date: Sat, 20 Apr 2019 22:14:00 -0700 Subject: [PATCH] Ensure query string parameters persist --- src/services/SiteSwitcherService.php | 30 ++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/services/SiteSwitcherService.php b/src/services/SiteSwitcherService.php index 5ba8c24..db34e10 100644 --- a/src/services/SiteSwitcherService.php +++ b/src/services/SiteSwitcherService.php @@ -29,25 +29,39 @@ class SiteSwitcherService extends Component /** * Render localized URL for current page. * - * @param null $siteHandle - * @param null $element + * @param string|null $siteHandle + * @param Element|null $element * @return bool|string + * @throws Exception + * @throws InvalidConfigException * @throws SiteNotFoundException */ - public function url($siteHandle = null, $element = null) + public function url($siteHandle = null, Element $element = null) { // If no site handle specified, use the default site if (!$siteHandle) { $siteHandle = Craft::$app->getSites()->getPrimarySite()->handle; } - // If element is specified, return element URL + // If element is specified if ($element) { - return $this->_getElementUrl($siteHandle, $element); + // Get element URL + $siteLink = $this->_getElementUrl($siteHandle, $element); + } else { + // Get non-element URL + $siteLink = $this->_getNonElementUrl($siteHandle); + } + + // Get query string + $queryString = Craft::$app->getRequest()->getQueryStringWithoutPath(); + + // If site link is valid and query string exists, append query string + if ($siteLink && $queryString) { + $siteLink .= "?{$queryString}"; } - // Return non-element URL - return $this->_getNonElementUrl($siteHandle); + // Return site link + return $siteLink; } /** @@ -55,7 +69,7 @@ public function url($siteHandle = null, $element = null) * * @param string $siteHandle * @param Element $element - * @return bool + * @return string|bool */ private function _getElementUrl($siteHandle, Element $element) {