diff --git a/src/Providers/ExtensionServiceProvider.php b/src/Providers/ExtensionServiceProvider.php index 38d756a6b8..10a0da94a3 100644 --- a/src/Providers/ExtensionServiceProvider.php +++ b/src/Providers/ExtensionServiceProvider.php @@ -160,6 +160,7 @@ class ExtensionServiceProvider extends ServiceProvider Tags\GetError::class, Tags\GetErrors::class, Tags\GetFiles::class, + Tags\GetSite::class, Tags\Glide::class, Tags\In::class, Tags\Increment::class, diff --git a/src/Tags/GetSite.php b/src/Tags/GetSite.php new file mode 100644 index 0000000000..82ac0bb72c --- /dev/null +++ b/src/Tags/GetSite.php @@ -0,0 +1,41 @@ +$var + : $site; + } + + /** + * {{ get_site handle="" }} ... {{ /get_site }}. + */ + public function index() + { + if (! $handle = $this->params->get('handle')) { + throw new \Exception('A site handle is required.'); + } + + if (! $site = Site::get($handle)) { + throw new \Exception("Site [$handle] does not exist."); + } + + return $site; + } +} diff --git a/tests/Tags/GetSiteTagTest.php b/tests/Tags/GetSiteTagTest.php new file mode 100644 index 0000000000..7cc1864dcd --- /dev/null +++ b/tests/Tags/GetSiteTagTest.php @@ -0,0 +1,59 @@ + [ + 'english' => ['name' => 'English', 'locale' => 'en_US', 'url' => '/en'], + 'french' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => '/fr'], + ]]); + } + + /** @test */ + public function it_gets_site_by_handle() + { + $this->assertEquals( + 'English', + Antlers::parse('{{ get_site handle="english" }}{{ name }}{{ /get_site }}') + ); + + $this->assertEquals( + 'French', + Antlers::parse('{{ get_site:french }}{{ name }}{{ /get_site:french }}') + ); + } + + /** @test */ + public function it_can_be_used_as_single_tag() + { + $this->assertEquals( + 'en_US', + Antlers::parse('{{ get_site:english:locale }}') + ); + } + + /** @test */ + public function it_throws_exception_if_handle_is_missing() + { + $this->expectExceptionMessage('A site handle is required.'); + + Antlers::parse('{{ get_site }}{{ name }}{{ /get_site }}'); + } + + /** @test */ + public function it_throws_exception_if_site_doesnt_exist() + { + $this->expectExceptionMessage('Site [nonexistent] does not exist.'); + + Antlers::parse('{{ get_site handle="nonexistent" }}{{ name }}{{ /get_site }}'); + } +}