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 get_site tag #9580

Merged
merged 10 commits into from
May 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Providers/ExtensionServiceProvider.php
Expand Up @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions src/Tags/GetSite.php
@@ -0,0 +1,41 @@
<?php

namespace Statamic\Tags;

use Illuminate\Support\Str;
use Statamic\Facades\Site;

class GetSite extends Tags
{
/**
* {{ get_site:* }} ... {{ /get_site:* }}.
*/
public function wildcard($tag)
{
$handle = Str::before($tag, ':');

if (! $site = Site::get($handle)) {
throw new \Exception("Site [$handle] does not exist.");
}

return (Str::contains($tag, ':') && ($var = Str::after($tag, ':')))
? $site->$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;
}
}
59 changes: 59 additions & 0 deletions tests/Tags/GetSiteTagTest.php
@@ -0,0 +1,59 @@
<?php

namespace Tests\Tags;

use Statamic\Facades\Antlers;
use Statamic\Facades\Site;
use Tests\TestCase;

class GetSiteTagTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

Site::setConfig(['sites' => [
'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 }}');
}
}