Skip to content

Commit

Permalink
[5.x] Add always_augment_to_query option (#11086)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <jason@pixelfear.com>
  • Loading branch information
jacksleight and jasonvarga authored Nov 21, 2024
1 parent cecf54c commit 54f3d3f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
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
|--------------------------------------------------------------------------
|
| By default, Statamic will augment relationship fields with max_items: 1
| to the result of a query, for example an Entry instance. Setting this
| to true will augment to the query builder instead of the result.
|
*/

'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

0 comments on commit 54f3d3f

Please sign in to comment.