Skip to content

Commit

Permalink
Merge pull request #6686 from getkirby/fix/6676-custom-router-fail
Browse files Browse the repository at this point in the history
Revert "Redirect language URLs with non-translated slugs"
  • Loading branch information
bastianallgeier authored Sep 19, 2024
2 parents d14bc47 + 1633412 commit 69b95b7
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 57 deletions.
20 changes: 1 addition & 19 deletions src/Cms/LanguageRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Kirby\Cms;

use Kirby\Filesystem\F;
use Kirby\Toolkit\Str;

class LanguageRoutes
{
Expand All @@ -30,26 +29,9 @@ public static function create(App $kirby): array
'pattern' => $language->pattern(),
'method' => 'ALL',
'env' => 'site',
'action' => function ($path = null) use ($kirby, $language) {
'action' => function ($path = null) use ($language) {
$result = $language->router()->call($path);

// redirect secondary-language pages that have
// been accessed with non-translated slugs in their path
// to their fully translated URL
if ($path !== null && $result instanceof Page) {
if (Str::endsWith($result->url(), $path) === false) {
$url = $result->url();
$query = $kirby->request()->query()->toString();

// preserve query across redirect
if (empty($query) === false) {
$url .= '?' . $query;
}

return $kirby->response()->redirect($url);
}
}

// explicitly test for null as $result can
// contain falsy values that should still be returned
if ($result !== null) {
Expand Down
38 changes: 0 additions & 38 deletions tests/Cms/Languages/LanguageRoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,42 +122,4 @@ public function testNotNextWhenFalsyReturn()
$this->assertSame(1, $d);
$this->assertSame(2, $e);
}

public function testRedirectWhenNonTranslatedSlugs()
{
$app = $this->app->clone([
'site' => [
'children' => [
[
'slug' => 'page1',
'translations' => [
[
'code' => 'en',
],
[
'code' => 'de',
'slug' => 'seite1',
]
]
]
]
],
'request' => [
'query' => [
'foo' => 'bar',
]
]
]);

$result = $app->call('page1');
$this->assertSame($app->page('page1'), $result);

$result = $app->call('de/page1');
$this->assertInstanceOf(Responder::class, $result);
$this->assertSame(302, $result->code());
$this->assertSame('/de/seite1?foo=bar', $result->header('Location'));

$result = $app->call('de/seite1');
$this->assertSame($app->page('page1'), $result);
}
}

0 comments on commit 69b95b7

Please sign in to comment.