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

Add @collection directive #2128

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Facades/Blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Statamic\Facades;

use Illuminate\Support\Facades\Facade;
use Statamic\View\Blade\Directives;

/**
* @method static mixed collection($expression)
*
* @see \Statamic\View\Blade\Directives
*/
class Blade extends Facade
{
protected static function getFacadeAccessor()
{
return Directives::class;
}
}
14 changes: 14 additions & 0 deletions src/Providers/ViewServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Illuminate\View\View;
use Statamic\Facades\Site;
Expand Down Expand Up @@ -55,5 +56,18 @@ public function boot()
});

ini_set('pcre.backtrack_limit', config('statamic.system.pcre_backtrack_limit', -1));

$this->bootDirectives();
}

private function bootDirectives()
{
Blade::directive('collection', function ($expression) {
return "<?php foreach (Statamic\Facades\Blade::collection(${expression}) as \$entry) { ?>";
});

Blade::directive('endcollection', function () {
return '<?php } ?>';
});
}
}
54 changes: 54 additions & 0 deletions src/View/Blade/Directives.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Statamic\View\Blade;

use Statamic\Facades\Collection as CollectionAPI;
use Statamic\Stache\Query\EntryQueryBuilder;
use Statamic\Support\Arr;

class Directives
{
private EntryQueryBuilder $collectionQuery;
private array $params;

public function collection(string $handle, array $params = [])
{
$this->params = $params;
$this->collectionQuery = CollectionAPI::find($handle)->queryEntries();

$this->filter();
$this->limit();
$this->orderBy();

return $this->collectionQuery->get()->toAugmentedArray();
}

private function filter()
{
if ($where = Arr::get($this->params, 'where')) {
foreach (explode(',', $where) as $condition) {
[$field, $value] = explode(':', $condition);

$this->collectionQuery->where(trim($field), trim($value));
}
}
}

private function limit()
{
if ($limit = Arr::get($this->params, 'limit')) {
$this->collectionQuery->limit($limit);
}
}

private function orderBy()
{
if ($orderBy = Arr::get($this->params, 'orderBy')) {
$sort = explode(':', $orderBy);
$field = $sort[0];
$direction = $sort[1] ?? 'asc';

$this->collectionQuery->orderBy($field, $direction);
}
}
}
15 changes: 15 additions & 0 deletions tests/View/Blade/DirectivesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Tests\View\Blade;

class DirectivesTest extends TestCase
{
/** @test */
public function does_display_correctly()
{
$blade = "@collection('foo')";
$expected = "<?php foreach (Statamic\Facades\Blade::collection('foo') as \$entry) { ?>";

$this->assertSame($expected, $this->blade->compileString($blade));
}
}
31 changes: 31 additions & 0 deletions tests/View/Blade/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tests\View\Blade;

use Tests\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
{
protected $blade;

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

$this->blade = app('blade.compiler');
}

// https://stevegrunwell.com/blog/custom-laravel-blade-directives/
protected function assertDirectiveOutput($expected, $expression, $variables = [], $message = '')
{
$compiled = $this->blade->compileString($expression);

ob_start();
extract($variables);
eval(' ?>'.$compiled.'<?php ');

$output = ob_get_clean();

$this->assertSame($expected, $output, $message);
}
}