Skip to content

Commit

Permalink
[4.x] Add children tag (#8990)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Dec 7, 2023
1 parent 1d0fa12 commit 45bf2a1
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Providers/ExtensionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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');

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

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

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

}
}

0 comments on commit 45bf2a1

Please sign in to comment.