From a11b0731f58ff96bcfa0df35a7467e33cb907338 Mon Sep 17 00:00:00 2001 From: Michael Aerni Date: Wed, 21 Feb 2024 11:22:37 -0500 Subject: [PATCH 1/2] Add ability to link to different site --- src/Tags/MountUrl.php | 6 +++++- tests/Tags/MountUrlTagTest.php | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Tags/MountUrl.php b/src/Tags/MountUrl.php index c6b6e0741d..be49aa54c0 100644 --- a/src/Tags/MountUrl.php +++ b/src/Tags/MountUrl.php @@ -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); } } diff --git a/tests/Tags/MountUrlTagTest.php b/tests/Tags/MountUrlTagTest.php index 861a9f5734..634537ebdd 100644 --- a/tests/Tags/MountUrlTagTest.php +++ b/tests/Tags/MountUrlTagTest.php @@ -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'], @@ -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" }}'); @@ -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)); From 1f69868a645ebfbf06497f80645e875a337c05fc Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 15 Aug 2024 10:46:54 -0400 Subject: [PATCH 2/2] output absolute url if its a different domain --- src/Tags/MountUrl.php | 12 ++++--- tests/Tags/MountUrlTagTest.php | 58 ++++++++++++++++++++++------------ 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/Tags/MountUrl.php b/src/Tags/MountUrl.php index be49aa54c0..6de47dacc0 100644 --- a/src/Tags/MountUrl.php +++ b/src/Tags/MountUrl.php @@ -29,10 +29,14 @@ private function mount($handle) return; } - $site = $this->params->get('site') ?? Site::current()->handle(); + $currentSite = Site::current(); + $site = $this->params->has('site') ? Site::get($this->params->get('site')) : $currentSite; - return $site == Site::current()->handle() - ? $collection->url($site) - : $collection->absoluteUrl($site); + // 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()); } } diff --git a/tests/Tags/MountUrlTagTest.php b/tests/Tags/MountUrlTagTest.php index 3d8484a67d..556c01bf48 100644 --- a/tests/Tags/MountUrlTagTest.php +++ b/tests/Tags/MountUrlTagTest.php @@ -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; @@ -19,43 +20,60 @@ 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(); } #[Test] - public function it_gets_collection_mount() + #[DataProvider('mountProvider')] + public function it_gets_url($currentSite, $template, $expected) { - $this->assertParseEquals('/pages/blog', '{{ mount_url:blog }}'); - $this->assertParseEquals('/pages/blog', '{{ mount_url handle="blog" }}'); - - Site::setCurrent('french'); - $this->assertParseEquals('/fr/le-pages/le-blog', '{{ mount_url:blog }}'); - $this->assertParseEquals('/fr/le-pages/le-blog', '{{ mount_url handle="blog" }}'); + Site::setCurrent($currentSite); + $this->assertParseEquals($expected, $template); } - #[Test] - public function it_can_link_to_selected_site() + public static function mountProvider() { - $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" }}'); + 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 = [])