Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add ability to set site on the mount_url tag #9561

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Tags/MountUrl.php
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
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();

$this->setSites([
'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