Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrestoffe committed Dec 3, 2020
2 parents 0d7bd3d + e423832 commit 86c6d48
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .craftplugin
@@ -1 +1 @@
{"pluginName":"Locale Redirector","pluginDescription":"Automatically redirect visitors to their preferred language","pluginVersion":"1.5.2","pluginAuthorName":"Pierre Stoffe","pluginVendorName":"pierrestoffe","pluginAuthorUrl":"https://pierrestoffe.be","pluginAuthorGithub":"pierrestoffe","codeComments":"yes","pluginComponents":["services"],"consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
{"pluginName":"Locale Redirector","pluginDescription":"Automatically redirect visitors to their preferred language","pluginVersion":"1.5.3","pluginAuthorName":"Pierre Stoffe","pluginVendorName":"pierrestoffe","pluginAuthorUrl":"https://pierrestoffe.be","pluginAuthorGithub":"pierrestoffe","codeComments":"yes","pluginComponents":["services"],"consolecommandName":"","controllerName":"","cpsectionName":"","elementName":"","fieldName":"","modelName":"","purchasableName":"","recordName":"","serviceName":"","taskName":"","utilityName":"","widgetName":"","apiVersion":"api_version_3_0"}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

## [1.5.3] - 2020-12-03

### Fixed
- Fixed issue where visitors where automatically redirected to the defaultEntryId when it is defined. The defaultEntryId should instead only be used when calling the language switcher variable.

## [1.5.2] - 2020-11-18

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -2,7 +2,7 @@
"name": "pierrestoffe/craft-language-redirector",
"description": "Automatically redirect visitors to their preferred language",
"type": "craft-plugin",
"version": "1.5.2",
"version": "1.5.3",
"keywords": [
"craft",
"cms",
Expand Down
28 changes: 21 additions & 7 deletions src/services/LanguageRedirectorService.php
Expand Up @@ -154,12 +154,13 @@ public function redirectCrawler() {
*
* @param string|null $language
* @param string|null $group
* @param bool $withDefault
*
* @return string
*/
public function getTargetUrl(string $language = null, string $group = null)
public function getTargetUrl(string $language = null, string $group = null, bool $withDefault = false)
{
$targetElement = $this->getTargetElement($language, $group);
$targetElement = $this->getTargetElement($language, $group, $withDefault);

if (null === $targetElement) {
return null;
Expand All @@ -181,13 +182,13 @@ public function getTargetUrl(string $language = null, string $group = null)
*
* @param string|null $language
* @param string|null $group
* @param bool $withDefault
*
* @return ElementInterface
*/
public function getTargetElement(string $language = null, string $group = null)
public function getTargetElement(string $language = null, string $group = null, bool $withDefault = false)
{
$targetSite = $this->getTargetSite($language, $group);
$defaultEntryId = LanguageRedirector::getInstance()->getSettings()->defaultEntryId;

$currentElement = Craft::$app->urlManager->getMatchedElement();

Expand All @@ -203,12 +204,25 @@ public function getTargetElement(string $language = null, string $group = null)
$targetElement = Craft::$app->elements->getElementById($currentElement->getId(), null, $targetSite->id);

// If element is not enabled for this site
if (null === $targetElement || (false == $targetElement->enabledForSite && null === $this->_getLanguageFromQueryParameter())) {
if (null === $targetElement || (false === $targetElement->enabledForSite && null === $this->_getLanguageFromQueryParameter())) {
$targetElementFound = false;
}

if (false === $targetElementFound && null !== $defaultEntryId) {
$targetElement = Craft::$app->elements->getElementById($defaultEntryId, null, $targetSite->id);
if ($withDefault) {
$defaultEntryId = LanguageRedirector::getInstance()->getSettings()->defaultEntryId;

if (false === $targetElementFound && null !== $defaultEntryId) {
$targetElement = Craft::$app->elements->getElementById($defaultEntryId, null, $targetSite->id);

$targetElementFound = true;
if (null === $targetElement || (false === $targetElement->enabledForSite && null === $this->_getLanguageFromQueryParameter())) {
$targetElementFound = false;
}
}
}

if (false === $targetElementFound) {
return null;
}

return $targetElement;
Expand Down
2 changes: 1 addition & 1 deletion src/variables/LanguageSwitcherVariable.php
Expand Up @@ -43,7 +43,7 @@ public function getUrls($urlOverrides = null, string $group = null)
$languages = array();

foreach ($siteLanguages as $language => $site) {
$targetUrl = $urlOverrides[$language] ?? $languageRedirectorService->getTargetUrl($language, $group);
$targetUrl = $urlOverrides[$language] ?? $languageRedirectorService->getTargetUrl($language, $group, true);
$locale = Craft::$app->i18n->getLocaleById($language);

if (null !== $targetUrl) {
Expand Down

0 comments on commit 86c6d48

Please sign in to comment.