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] Add always_augment_to_query option #11086

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
13 changes: 13 additions & 0 deletions config/system.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@

'update_references' => true,

/*
|--------------------------------------------------------------------------
| Always Augment to Query
|--------------------------------------------------------------------------
|
| With this enabled, Statamic will always augment relationship fields to
| query builders. With it disabled `max_items: 1` fields are augmented to the
| referenced entry object.
|
*/

'always_augment_to_query' => false,

/*
|--------------------------------------------------------------------------
| Row ID handle
Expand Down
4 changes: 3 additions & 1 deletion src/Fieldtypes/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ public function augment($values)

$query = new OrderedQueryBuilder($query, $ids);

return $single ? Blink::once($key, fn () => $query->first()) : $query;
return $single && ! config('statamic.system.always_augment_to_query', false)
? Blink::once($key, fn () => $query->first())
: $query;
}

public function shallowAugment($values)
Expand Down
4 changes: 3 additions & 1 deletion src/Fieldtypes/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,9 @@ public function augment($values)

$query = $this->queryBuilder($values);

return $single ? Blink::once($key, fn () => $query->first()) : $query;
return $single && ! config('statamic.system.always_augment_to_query', false)
? Blink::once($key, fn () => $query->first())
: $query;
}

public function shallowAugment($values)
Expand Down
4 changes: 3 additions & 1 deletion src/Fieldtypes/Terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ public function augment($values)

$query = $this->queryBuilder($values);

return $single ? Blink::once($key, fn () => $query->first()) : $query;
return $single && ! config('statamic.system.always_augment_to_query', false)
? Blink::once($key, fn () => $query->first())
: $query;
}

private function queryBuilder($values)
Expand Down
6 changes: 5 additions & 1 deletion src/Fieldtypes/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,15 @@ protected function getItemsForPreProcessIndex($values): Collection

public function augment($values)
{
$single = $this->config('max_items') === 1;

$ids = Arr::wrap($values);

$query = (new OrderedQueryBuilder(User::query(), $ids))->whereIn('id', $ids);

return $this->config('max_items') === 1 ? $query->first() : $query;
return $single && ! config('statamic.system.always_augment_to_query', false)
? $query->first()
: $query;
}

public function shallowAugment($values)
Expand Down
Loading