Skip to content

Commit

Permalink
Fix Url:to with link anchors #2280
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico Hoffmann committed Dec 15, 2019
1 parent c94664c commit 5174c54
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 0 additions & 2 deletions config/routes.php
Expand Up @@ -5,8 +5,6 @@
use Kirby\Cms\Panel;
use Kirby\Cms\PanelPlugins;
use Kirby\Cms\PluginAssets;
use Kirby\Http\Response\Redirect;
use Kirby\Http\Router\Route;
use Kirby\Toolkit\Str;

return function ($kirby) {
Expand Down
14 changes: 12 additions & 2 deletions src/Cms/Url.php
Expand Up @@ -3,6 +3,8 @@
namespace Kirby\Cms;

use Kirby\Http\Url as BaseUrl;
use Kirby\Toolkit\Str;


/**
* The `Url` class extends the
Expand Down Expand Up @@ -76,8 +78,16 @@ public static function to(string $path = null, $options = null): string
}

// get a language url for the linked page, if the page can be found
if ($kirby->multilang() === true && $page = page($path)) {
$path = $page->url($language);
if ($kirby->multilang() === true) {
$parts = Str::split($path, '#');

if ($page = page($parts[0] ?? null)) {
$path = $page->url($language);

if (isset($parts[1]) === true) {
$path .= '#' . $parts[1];
}
}
}

return $kirby->component('url')($kirby, $path, $options, function (string $path = null, $options = null) {
Expand Down
30 changes: 30 additions & 0 deletions tests/Cms/KirbyText/KirbyTagsTest.php
Expand Up @@ -251,6 +251,36 @@ public function testLinkWithLangAttribute()
$this->assertEquals('<a href="https://getkirby.com/de/a">getkirby.com/de/a</a>', $app->kirbytags('(link: a lang: de)'));
}

public function testLinkWithHash()
{
$app = new App([
'roots' => [
'index' => '/dev/null'
],
'urls' => [
'index' => 'https://getkirby.com'
],
'languages' => [
'en' => [
'code' => 'en'
],
'de' => [
'code' => 'de'
]
],
'site' => [
'children' => [
['slug' => 'a']
]
]
]);

$this->assertEquals('<a href="https://getkirby.com/en/a">getkirby.com/en/a</a>', $app->kirbytags('(link: a)'));
$this->assertEquals('<a href="https://getkirby.com/de/a">getkirby.com/de/a</a>', $app->kirbytags('(link: a lang: de)'));
$this->assertEquals('<a href="https://getkirby.com/en/a#anchor">getkirby.com/en/a</a>', $app->kirbytags('(link: a#anchor lang: en)'));
$this->assertEquals('<a href="https://getkirby.com/de/a#anchor">getkirby.com/de/a</a>', $app->kirbytags('(link: a#anchor lang: de)'));
}

public function testHooks()
{
$app = new App([
Expand Down

0 comments on commit 5174c54

Please sign in to comment.