From a081b97f5364e0a0fcdf61ba81cd487c876568e2 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 28 Nov 2024 06:55:42 +0000 Subject: [PATCH 1/2] Ensure no queries are run when entries field has no values --- src/Fieldtypes/Entries.php | 5 +++++ src/Query/EmptyQueryBuilder.php | 16 ++++++++++++++++ tests/Fieldtypes/EntriesTest.php | 6 ++++++ 3 files changed, 27 insertions(+) create mode 100644 src/Query/EmptyQueryBuilder.php diff --git a/src/Fieldtypes/Entries.php b/src/Fieldtypes/Entries.php index 29a9b96e78..d94b689bb5 100644 --- a/src/Fieldtypes/Entries.php +++ b/src/Fieldtypes/Entries.php @@ -18,6 +18,7 @@ use Statamic\Facades\User; use Statamic\Http\Resources\CP\Entries\EntriesFieldtypeEntries; use Statamic\Http\Resources\CP\Entries\EntriesFieldtypeEntry as EntryResource; +use Statamic\Query\EmptyQueryBuilder; use Statamic\Query\OrderedQueryBuilder; use Statamic\Query\Scopes\Filter; use Statamic\Query\Scopes\Filters\Concerns\QueriesFilters; @@ -337,6 +338,10 @@ protected function collect($value) private function queryBuilder($values) { + if (! $values) { + return new StatusQueryBuilder(new EmptyQueryBuilder()); + } + $site = Site::current()->handle(); if (($parent = $this->field()->parent()) && $parent instanceof Localization) { $site = $parent->locale(); diff --git a/src/Query/EmptyQueryBuilder.php b/src/Query/EmptyQueryBuilder.php new file mode 100644 index 0000000000..8ec78ac758 --- /dev/null +++ b/src/Query/EmptyQueryBuilder.php @@ -0,0 +1,16 @@ +assertInstanceOf(Builder::class, $augmented); $this->assertCount(0, $augmented->get()); + + $augmented = $this->fieldtype()->augment([]); + + $this->assertInstanceOf(Builder::class, $augmented); + $this->assertCount(0, $augmented->get()); } #[Test] From 4a82284f34944459b701e59be0832268ca11a4f7 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 28 Nov 2024 06:59:18 +0000 Subject: [PATCH 2/2] :beer: --- tests/Fieldtypes/EntriesTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Fieldtypes/EntriesTest.php b/tests/Fieldtypes/EntriesTest.php index f50b48f108..6407d2a4a0 100644 --- a/tests/Fieldtypes/EntriesTest.php +++ b/tests/Fieldtypes/EntriesTest.php @@ -15,7 +15,6 @@ use Statamic\Facades\Site; use Statamic\Fields\Field; use Statamic\Fieldtypes\Entries; -use Statamic\Query\EmptyQueryBuilder; use Tests\Fieldtypes\Concerns\TestsQueryableValueWithMaxItems; use Tests\PreventSavingStacheItemsToDisk; use Tests\TestCase;