Skip to content

Commit 8267d7b

Browse files
committed
Use hooks to set query scope automatically
1 parent a5ba5e3 commit 8267d7b

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ By using `pluck('id')`, you ensure that the returned data is a flat array of IDs
4141
Here’s how to apply this advanced filter within your template:
4242

4343
```antlers
44-
{{ collection:articles query_scope="filter_builder" :filter_builder="my_filter_builder_field_handle" }}
44+
{{ collection:articles :filter_builder="my_filter_builder_field_handle" }}
4545
<!-- Articles that match the locations of the current page will be displayed -->
4646
{{ /collection:articles }}
47+
4748
```
49+
Note: the filter builder requires a query scope behind the scenes, so will not work alongside a query_scope parameter.
50+
4851
You may combine the dynamic Filter Builder query with filter parameters.
4952

5053
The Statamic Filter Builder addon is your pathway to creating a responsive and context-aware website, where content curation is as intelligent as it is effortless.

src/ServiceProvider.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tv2regionerne\StatamicFilterBuilder;
44

55
use Statamic\Providers\AddonServiceProvider;
6+
use Statamic\Tags\Collection\Collection as CollectionTag;
67

78
class ServiceProvider extends AddonServiceProvider
89
{
@@ -20,4 +21,30 @@ class ServiceProvider extends AddonServiceProvider
2021
'resources/js/addon.js',
2122
],
2223
];
24+
25+
public function boot()
26+
{
27+
$this->addCollectionHook();
28+
}
29+
30+
private function addCollectionHook(): self
31+
{
32+
CollectionTag::hook('init', function ($value, $next) {
33+
if (! $this->params->get('filter_builder')) {
34+
return $next($value);
35+
}
36+
37+
if ($this->params->get('query_scope')) {
38+
return $next($value);
39+
}
40+
41+
$this->params = $this->params->merge([
42+
'query_scope' => 'filter_builder',
43+
]);
44+
45+
return $next($value);
46+
});
47+
48+
return $this;
49+
}
2350
}

tests/TestCase.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,25 @@
33
namespace Tests;
44

55
use Statamic\Testing\AddonTestCase;
6+
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;
67
use Tv2regionerne\StatamicFilterBuilder\ServiceProvider;
78

89
class TestCase extends AddonTestCase
910
{
11+
use PreventsSavingStacheItemsToDisk;
12+
13+
protected $fakeStacheDirectory = __DIR__.'/__fixtures__/dev-null';
14+
1015
protected string $addonServiceProvider = ServiceProvider::class;
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->withoutVite();
22+
23+
if (! file_exists($this->fakeStacheDirectory)) {
24+
mkdir($this->fakeStacheDirectory, 0777, true);
25+
}
26+
}
1127
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use Statamic\Facades;
4+
use Statamic\Tags\Collection\Collection as CollectionTag;
5+
6+
uses(Tests\TestCase::class);
7+
8+
beforeEach(function () {
9+
Facades\Collection::make()
10+
->handle('pages')
11+
->save();
12+
});
13+
14+
it('adds query scope when none is applied', function () {
15+
$params = [];
16+
CollectionTag::hook('fetched-entries', function () use (&$params) {
17+
$params = $this->params->all();
18+
});
19+
20+
Facades\Antlers::parse('{{ collection:pages filter_builder="something" }}{{ title }}{{ /collection:pages }}');
21+
22+
$this->assertArrayHasKey('query_scope', $params);
23+
});
24+
25+
it('doesnt add a query scope if one already exists', function () {
26+
$params = [];
27+
CollectionTag::hook('fetched-entries', function () use (&$params) {
28+
$params = $this->params->all();
29+
});
30+
31+
Facades\Antlers::parse('{{ collection:pages filter_builder="something" query_scope="some_scope" }}{{ title }}{{ /collection:pages }}');
32+
33+
$this->assertArrayHasKey('query_scope', $params);
34+
$this->assertNotSame('filter_builder', $params['query_scope']);
35+
});
36+
37+
it('doesn\'t add query scope when no filter builder param is added', function () {
38+
$params = [];
39+
CollectionTag::hook('fetched-entries', function () use (&$params) {
40+
$params = $this->params->all();
41+
});
42+
43+
Facades\Antlers::parse('{{ collection:pages }}{{ title }}{{ /collection:pages }}');
44+
45+
$this->assertArrayNotHasKey('query_scope', $params);
46+
});

0 commit comments

Comments
 (0)