From 917b85415c9f4e9b8f8f9766039633f240a005fe Mon Sep 17 00:00:00 2001 From: Jack Sleight Date: Thu, 3 Oct 2024 16:00:55 +0100 Subject: [PATCH] Add always augment to query option --- config/system.php | 13 +++++++++++++ src/Fieldtypes/Assets/Assets.php | 4 +++- src/Fieldtypes/Entries.php | 4 +++- src/Fieldtypes/Terms.php | 4 +++- src/Fieldtypes/Users.php | 6 +++++- 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/config/system.php b/config/system.php index 0bc9192b40..b3fc2043c7 100644 --- a/config/system.php +++ b/config/system.php @@ -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 diff --git a/src/Fieldtypes/Assets/Assets.php b/src/Fieldtypes/Assets/Assets.php index f44d58b305..3c78bb56d4 100644 --- a/src/Fieldtypes/Assets/Assets.php +++ b/src/Fieldtypes/Assets/Assets.php @@ -264,7 +264,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) diff --git a/src/Fieldtypes/Entries.php b/src/Fieldtypes/Entries.php index 48b2795c9d..522b0e5780 100644 --- a/src/Fieldtypes/Entries.php +++ b/src/Fieldtypes/Entries.php @@ -368,7 +368,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) diff --git a/src/Fieldtypes/Terms.php b/src/Fieldtypes/Terms.php index 3b4770999e..9d7a576c34 100644 --- a/src/Fieldtypes/Terms.php +++ b/src/Fieldtypes/Terms.php @@ -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) diff --git a/src/Fieldtypes/Users.php b/src/Fieldtypes/Users.php index b3e9317701..641d7d99f5 100644 --- a/src/Fieldtypes/Users.php +++ b/src/Fieldtypes/Users.php @@ -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)