Much improved multi-language support for pages

Added `Admin::redirect()` method to allow redirects to be used outside of controllers
Added `$admin->adminRoute()` method and `admin_route()` twig function to create language aware admin page links
Renamed `Admin::route()` to `Admin::getCurrentRoute()` and deprecated the old call
Admin redirects should now work better with multiple languages enabled
Fixed default language being renamed to `page.en.md` (English) instead of keeping existing `page.md` filename
This commit is contained in:
Matias Griese
2019-06-27 13:19:30 +03:00
parent d626305f11
commit 8872455e1a
11 changed files with 245 additions and 158 deletions

View File

@@ -609,48 +609,12 @@ class AdminBaseController
/**
* Redirect to the route stored in $this->redirect
*
* Route may or may not be prefixed by /en or /admin or /en/admin.
*/
public function redirect()
{
if (!$this->redirect) {
return;
}
$base = $this->admin->base;
$this->redirect = '/' . ltrim($this->redirect, '/');
$multilang = $this->isMultilang();
$redirect = '';
if ($multilang) {
// if base path does not already contain the lang code, add it
$langPrefix = '/' . $this->grav['session']->admin_lang;
if (!Utils::startsWith($base, $langPrefix . '/')) {
$base = $langPrefix . $base;
}
// now the first 4 chars of base contain the lang code.
// if redirect path already contains the lang code, and is != than the base lang code, then use redirect path as-is
if (Utils::pathPrefixedByLangCode($base) && Utils::pathPrefixedByLangCode($this->redirect)
&& !Utils::startsWith($this->redirect, $base)
) {
$redirect = $this->redirect;
} else {
if (!Utils::startsWith($this->redirect, $base)) {
$this->redirect = $base . $this->redirect;
}
}
} else {
if (!Utils::startsWith($this->redirect, $base)) {
$this->redirect = $base . $this->redirect;
}
}
if (!$redirect) {
$redirect = $this->redirect;
}
$this->grav->redirect($redirect, $this->redirectCode);
$this->admin->redirect($this->redirect, $this->redirectCode);
}
/**