Skip to content

Commit

Permalink
Merge branch 'master' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrestoffe committed Jul 28, 2018
2 parents 4b75389 + d5fa7b4 commit bfe1de6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .craftplugin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"pluginName":"Locale Redirector","pluginDescription":"Automatically redirect visitors to their preferred language","pluginVersion":"1.0.6","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.0.7","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"}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

## [1.0.7] - 2018-07-28

### Changed
- Initialize the language match only after the app is fully initialized

### Fixed
- Fixed a bug that would occur if the currently-visited entry wasn’t enable in all Sites

## [1.0.6] - 2018-07-12

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "pierrestoffe/craft-language-redirector",
"description": "Automatically redirect visitors to their preferred language",
"type": "craft-plugin",
"version": "1.0.6",
"version": "1.0.7",
"keywords": [
"craft",
"cms",
Expand Down
30 changes: 19 additions & 11 deletions src/LanguageRedirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,26 @@ public function init()
{
parent::init();

$request = Craft::$app->getRequest();
if (
$request->isSiteRequest &&
!$request->isConsoleRequest &&
!$request->isActionRequest &&
!$request->isLivePreview &&
!$request->isAjax
) {
$service = new LanguageRedirectorService();
$service->redirectVisitor();
}
// Check for the best language match
Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
function (Event $event) {
$request = Craft::$app->getRequest();
if (
$request->isSiteRequest &&
!$request->isConsoleRequest &&
!$request->isActionRequest &&
!$request->isLivePreview &&
!$request->isAjax
) {
$service = new LanguageRedirectorService();
$service->redirectVisitor();
}
}
);

// Register the variable
Event::on(
CraftVariable::class,
CraftVariable::EVENT_INIT,
Expand Down
4 changes: 4 additions & 0 deletions src/services/LanguageRedirectorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public function getTargetElement(string $language = null)

$targetElement = Craft::$app->elements->getElementById($currentElement->getId(), null, $targetSite->id);

if (null === $targetElement) {
return null;
}

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

0 comments on commit bfe1de6

Please sign in to comment.