cms
cms copied to clipboard
toAugmentedArray with nested EntryCollection breaks in v3.3.x
Bug description
Augmentation of EntryCollection
behaves different in v3.3.x compared to v3.2.x.
When augmenting the page with toAugmentedArray
and returning as JSON either an empty object or only a list of entry IDs is returned.
Expected a return value of list of objects (with all augmented data from entry).
If I run toAugmentedArray()
directly on the EntryCollection
it works just fine, so the error happens when it's nested within another field or page.
How to reproduce
Case: 1️⃣ only list of entry IDs
- make blueprint with Entries field
- call
json_encode($page->toAugmentedArray())
on aStatamic\Structures\Page
- get list of IDs
Case: 2️⃣ empty Object This is a bit more difficult to reproduce because the entries field in other fields
- make blueprint with nested fields
- Page > Bard > BardSet > Replicator > ReplicatorSet > Entries
Logs
No response
Environment
Environment
Laravel Version: 9.23.0
PHP Version: 8.1.8
Composer Version: 2.3.10
Environment: local
Debug Mode: ENABLED
Maintenance Mode: OFF
Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED
Statamic
Addons: 0
Antlers: regex
Version: 3.3.25 PRO
Also tested with Laravel 8 - no difference
Installation
Fresh statamic/statamic site via CLI
Antlers Parser
No response
Additional details
No response
I run into the same Problem after upgrading to 3.3. Taxonomies were only returned as IDs. After a deep dive into the source, I noticed that we always need to say which properties are relations and should be resolved.
The following works for me (Over all entries there are only two properties that could contain a Taxonomy, which should be declared as relation when augmenting):
$entry->toAugmentedCollection()->withRelations(['category', 'departments'])
The following Line is causing this behaviour: https://github.com/statamic/cms/blob/3.3/src/Data/AugmentedCollection.php#L54
$entry->toAugmentedCollection()->withRelations(['category', 'departments'])
This works well for the 1️⃣ case, where the Entries field is placed at the top 👍🏼
In the case 2️⃣ with Entries field within a Replicator or a Bard it doesn't work.
I dived into the code to find the problem of 2️⃣
The problem occurs if Statamic\Fields\Value
tries to get the value() when $value->isRelationship()
equal true.
In the case with an Entries field, $value->jsonSerialize()
returns a value of type Statamic\Query\OrderedQueryBuilder
, which serializes to {}
.
A quick fix would be to resolve this query builder in https://github.com/statamic/cms/blob/3.3/src/Fields/Value.php like
#[\ReturnTypeWillChange]
public function jsonSerialize($options = 0)
{
$value = $this->value();
/* TODO Add this line - resolve query builder*/
if ($value instanceof OrderedQueryBuilder) {
$value = $value->get();
}
if ($value instanceof Augmentable || $value instanceof Collection) {
$value = $value->toAugmentedArray();
}
return $value;
}
This line is inspired by https://github.com/statamic/cms/blob/626f876d91c0feac9f8af6994c689b45f5128aa6/src/Data/AugmentedCollection.php#L71
I also tested to check against Statamic\Contracts\Query\Builder as StatamicQueryBuilder
like in AugmentedCollection but got empty result 🤷🏼
Hope this helps to find a solution.
Related or duplicated of issues addressed in PR https://github.com/statamic/cms/pull/6229
Closing as duplicate of #6229.