Skip to content

Commit

Permalink
Add ability to link to different site
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Feb 21, 2024
1 parent 78d8f4b commit a11b073
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Tags/MountUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ private function mount($handle)
return;
}

return $collection->url(Site::current()->handle());
$site = $this->params->get('site') ?? Site::current()->handle();

return $site == Site::current()->handle()
? $collection->url($site)
: $collection->absoluteUrl($site);
}
}
24 changes: 22 additions & 2 deletions tests/Tags/MountUrlTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class MountUrlTagTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

/** @test */
public function it_gets_collection_mount()
public function setUp(): void
{
parent::setUp();

Site::setConfig(['sites' => [
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'],
Expand All @@ -25,10 +26,16 @@ public function it_gets_collection_mount()
'english' => 'pages/{slug}',
'french' => 'le-pages/{slug}',
])->save();

$mountEn = EntryFactory::collection('pages')->slug('blog')->locale('english')->id('blog-en')->create();
$mountFr = EntryFactory::collection('pages')->slug('le-blog')->locale('french')->origin('blog-en')->id('blog-fr')->create();

Collection::make('blog')->routes('{mount}/{slug}')->mount($mountEn->id())->save();
}

/** @test */
public function it_gets_collection_mount()
{
$this->assertParseEquals('/pages/blog', '{{ mount_url:blog }}');
$this->assertParseEquals('/pages/blog', '{{ mount_url handle="blog" }}');

Expand All @@ -37,6 +44,19 @@ public function it_gets_collection_mount()
$this->assertParseEquals('/fr/le-pages/le-blog', '{{ mount_url handle="blog" }}');
}

/** @test */
public function it_can_link_to_selected_site()
{
$this->assertParseEquals('/pages/blog', '{{ mount_url:blog }}');
$this->assertParseEquals('/pages/blog', '{{ mount_url:blog site="english" }}');
$this->assertParseEquals('http://localhost/fr/le-pages/le-blog', '{{ mount_url:blog site="french" }}');

Site::setCurrent('french');
$this->assertParseEquals('/fr/le-pages/le-blog', '{{ mount_url:blog }}');
$this->assertParseEquals('/fr/le-pages/le-blog', '{{ mount_url:blog site="french" }}');
$this->assertParseEquals('http://localhost/pages/blog', '{{ mount_url:blog site="english" }}');
}

private function assertParseEquals($expected, $template, $context = [])
{
$this->assertEquals($expected, (string) Antlers::parse($template, $context));
Expand Down

0 comments on commit a11b073

Please sign in to comment.