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

[5.x] Ensure no queries are run when entries field has no values #11184

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/Fieldtypes/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions src/Query/EmptyQueryBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Statamic\Query;

class EmptyQueryBuilder extends IteratorBuilder
{
protected function getBaseItems()
{
return collect([]);
}

public function whereStatus($status)
{
return $this;
}
}
5 changes: 5 additions & 0 deletions tests/Fieldtypes/EntriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ public function it_augments_to_a_query_builder_when_theres_no_value()

$this->assertInstanceOf(Builder::class, $augmented);
$this->assertCount(0, $augmented->get());

$augmented = $this->fieldtype()->augment([]);

$this->assertInstanceOf(Builder::class, $augmented);
$this->assertCount(0, $augmented->get());
}

#[Test]
Expand Down