-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/5'
- Loading branch information
Showing
4 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace TractorCow\Fluent\Middleware; | ||
|
||
use SilverStripe\Admin\AdminRootController; | ||
use SilverStripe\Control\Controller; | ||
use SilverStripe\Control\Director; | ||
use SilverStripe\Control\HTTPRequest; | ||
use SilverStripe\Control\HTTPResponse; | ||
use SilverStripe\Control\HTTPResponse_Exception; | ||
use SilverStripe\Control\Middleware\HTTPMiddleware; | ||
use TractorCow\Fluent\Extension\FluentDirectorExtension; | ||
use TractorCow\Fluent\State\FluentState; | ||
|
||
/** | ||
* This middleware will safely redirect you to the cms landing page when switching locales, | ||
* in case you were viewing a url that does not apply to the new locale. | ||
*/ | ||
class LocaleSwitchRedirector implements HTTPMiddleware | ||
{ | ||
public function process(HTTPRequest $request, callable $delegate) | ||
{ | ||
// Get and capture any possible errors | ||
try { | ||
$response = $delegate($request); | ||
} catch (HTTPResponse_Exception $ex) { | ||
$response = $ex->getResponse(); | ||
} | ||
|
||
// Check if this is a 404 error attempting when switching locales in the CMS | ||
$state = FluentState::singleton(); | ||
if ($response->getStatusCode() === 404 | ||
&& !$state->getIsFrontend() | ||
&& $this->getParamLocale($request) | ||
) { | ||
// Redirect to the CMS home page if the requested page doesn't exist | ||
$response = new HTTPResponse(); | ||
$response->redirect(Controller::join_links( | ||
Director::baseURL(), | ||
AdminRootController::admin_url() | ||
)); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Get locale from the query_param | ||
* | ||
* @param HTTPRequest $request | ||
* @return mixed | ||
*/ | ||
protected function getParamLocale(HTTPRequest $request) | ||
{ | ||
$queryParam = FluentDirectorExtension::config()->get('query_param'); | ||
return (string)($request->param($queryParam) ?: $request->requestVar($queryParam)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters