Skip to content

Commit

Permalink
Ensure query string parameters persist
Browse files Browse the repository at this point in the history
  • Loading branch information
lindseydiloreto committed Apr 21, 2019
1 parent 549fd09 commit d41bcf0
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/services/SiteSwitcherService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,47 @@ 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;
}

/**
* Get localized element URL.
*
* @param string $siteHandle
* @param Element $element
* @return bool
* @return string|bool
*/
private function _getElementUrl($siteHandle, Element $element)
{
Expand Down

0 comments on commit d41bcf0

Please sign in to comment.