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

[4.x] Add children tag #8990

Merged
merged 6 commits into from
Dec 7, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class ExtensionServiceProvider extends ServiceProvider
Tags\Assets::class,
Tags\Cache::class,
Tags\Can::class,
Tags\Children::class,
Tags\Collection\Collection::class,
Tags\Cookie::class,
Tags\Dd::class,
Expand Down
25 changes: 25 additions & 0 deletions src/Tags/Children.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Statamic\Tags;

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

class Children extends Structure
{
/**
* The {{ children }} tag.
*
* Get any children of the current url
*
* @return string
*/
public function index()
{
$this->params->put('from', Str::start(Str::after(URL::getCurrent(), Site::current()->url()), '/'));
$this->params->put('max_depth', 1);

return $this->structure($this->params->get('handle', 'collection::pages'));
}
}
101 changes: 101 additions & 0 deletions tests/Tags/ChildrenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Tests\Tags;

use Facades\Tests\Factories\EntryFactory;
use Mockery;
use Statamic\Facades\Collection;
use Statamic\Facades\Parse;
use Statamic\Facades\Site;
use Statamic\Tags\Children;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

class ChildrenTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

public function setUp(): void
{
parent::setUp();

Site::setConfig(['sites' => [
'en' => ['url' => '/', 'locale' => 'en_US'],
'fr' => ['url' => '/fr/', 'locale' => 'fr_FR'],
]]);
}

private function tag($tag, $data = [])
{
return (string) Parse::template($tag, $data);
}

private function setUpEntries()
{
$collection = tap(Collection::make('pages')->sites(['en', 'fr'])->routes('{slug}')->structureContents(['root' => false]))->save();

EntryFactory::collection('pages')->id('foo')->data([
'title' => 'the foo entry',
])->create();

EntryFactory::collection('pages')->id('bar')->data([
'title' => 'the bar entry',
])->create();

EntryFactory::collection('pages')->id('fr-foo')->origin('foo')->locale('fr')->data([
'title' => 'the french foo entry',
])->create();

EntryFactory::collection('pages')->id('fr-bar')->origin('foo')->locale('fr')->data([
'title' => 'the french bar entry',
])->create();

$collection->structure()->in('en')->tree([
['entry' => 'foo', 'url' => '/foo', 'children' => [
['entry' => 'bar', 'url' => '/foo/bar'],
]],
])->save();

$collection->structure()->in('fr')->tree([
['entry' => 'fr-foo', 'url' => '/fr-foo', 'children' => [
['entry' => 'fr-bar', 'url' => '/fr-foo/fr-bar'],
]],
])->save();
}

/** @test */
public function it_gets_children_data()
{
$this->setUpEntries();

$this->get('/foo');

$this->assertEquals('the bar entry', $this->tag('{{ children }}{{ title }}{{ /children }}'));
}

/** @test */
public function it_gets_children_data_when_in_another_site()
{
$this->setUpEntries();

$this->get('/fr/fr-foo');

$this->assertEquals('the french bar entry', $this->tag('{{ children }}{{ title }}{{ /children }}'));
}

/** @test */
public function it_doesnt_affect_children_in_nav()
{
$this->setUpEntries();

$mock = Mockery::mock(Children::class);
$mock->shouldNotReceive('index');
ryanmitchell marked this conversation as resolved.
Show resolved Hide resolved

$this->app['statamic.tags']['children'] = $mock;

$this->get('/foo');

$this->assertEquals('the bar entry', $this->tag('{{ nav }}{{ children }}{{ title }}{{ /children }}{{ /nav }}'));

}
}