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

Merged
merged 4 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Tags/MountUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ private function mount($handle)
return;
}

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

// If the target site is on a different domain, return an absolute URL.
$isDifferentDomain = parse_url($site->absoluteUrl(), PHP_URL_HOST) !== parse_url($currentSite->absoluteUrl(), PHP_URL_HOST);

return $isDifferentDomain
? $collection->absoluteUrl($site->handle())
: $collection->url($site->handle());
}
}
58 changes: 48 additions & 10 deletions tests/Tags/MountUrlTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Tags;

use Facades\Tests\Factories\EntryFactory;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Antlers;
use Statamic\Facades\Collection;
Expand All @@ -14,28 +15,65 @@ 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'],
'english' => ['url' => 'http://example.com/', 'locale' => 'en'],
'french' => ['url' => 'http://example.com/fr/', 'locale' => 'fr'],
'german' => ['url' => 'http://example.de/', 'locale' => 'de'],
]);

Collection::make('pages')->sites(['english', 'french'])->routes([
Collection::make('pages')->sites(['english', 'french', 'german'])->routes([
'english' => 'pages/{slug}',
'french' => 'le-pages/{slug}',
'german' => 'der-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();
$mountDe = EntryFactory::collection('pages')->slug('der-blog')->locale('german')->origin('blog-en')->id('blog-de')->create();

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

$this->assertParseEquals('/pages/blog', '{{ mount_url:blog }}');
$this->assertParseEquals('/pages/blog', '{{ mount_url handle="blog" }}');
#[Test]
#[DataProvider('mountProvider')]
public function it_gets_url($currentSite, $template, $expected)
{
Site::setCurrent($currentSite);
$this->assertParseEquals($expected, $template);
}

Site::setCurrent('french');
$this->assertParseEquals('/fr/le-pages/le-blog', '{{ mount_url:blog }}');
$this->assertParseEquals('/fr/le-pages/le-blog', '{{ mount_url handle="blog" }}');
public static function mountProvider()
{
return [
['english', '{{ mount_url:blog }}', '/pages/blog'],
['english', '{{ mount_url:blog site="english" }}', '/pages/blog'],
['english', '{{ mount_url:blog site="french" }}', '/fr/le-pages/le-blog'],
['english', '{{ mount_url:blog site="german" }}', 'http://example.de/der-pages/der-blog'],
['french', '{{ mount_url:blog }}', '/fr/le-pages/le-blog'],
['french', '{{ mount_url:blog site="english" }}', '/pages/blog'],
['french', '{{ mount_url:blog site="french" }}', '/fr/le-pages/le-blog'],
['french', '{{ mount_url:blog site="german" }}', 'http://example.de/der-pages/der-blog'],
['german', '{{ mount_url:blog }}', '/der-pages/der-blog'],
['german', '{{ mount_url:blog site="english" }}', 'http://example.com/pages/blog'],
['german', '{{ mount_url:blog site="french" }}', 'http://example.com/fr/le-pages/le-blog'],
['german', '{{ mount_url:blog site="german" }}', '/der-pages/der-blog'],
['english', '{{ mount_url handle="blog" }}', '/pages/blog'],
['english', '{{ mount_url handle="blog" site="english" }}', '/pages/blog'],
['english', '{{ mount_url handle="blog" site="french" }}', '/fr/le-pages/le-blog'],
['english', '{{ mount_url handle="blog" site="german" }}', 'http://example.de/der-pages/der-blog'],
['french', '{{ mount_url handle="blog" }}', '/fr/le-pages/le-blog'],
['french', '{{ mount_url handle="blog" site="english" }}', '/pages/blog'],
['french', '{{ mount_url handle="blog" site="french" }}', '/fr/le-pages/le-blog'],
['french', '{{ mount_url handle="blog" site="german" }}', 'http://example.de/der-pages/der-blog'],
['german', '{{ mount_url handle="blog" }}', '/der-pages/der-blog'],
['german', '{{ mount_url handle="blog" site="english" }}', 'http://example.com/pages/blog'],
['german', '{{ mount_url handle="blog" site="french" }}', 'http://example.com/fr/le-pages/le-blog'],
['german', '{{ mount_url handle="blog" site="german" }}', '/der-pages/der-blog'],
];
}

private function assertParseEquals($expected, $template, $context = [])
Expand Down
Loading