ideas
ideas copied to clipboard
Ignore nested entries when augmenting - Livewire problems
EntryCollections need to be converted to arrays when working with Livewire, but toArray() calls can produce infinite loops when the entries contain references to other entries and toAugmentedArray() is not suitable for Livewire due to the data types it returns.
I've run into this problem before.
Here's a simplified version of my problem:
$events = Entry::query()
->where('collection', 'events')
->where('published', true)
->get()
// I then perform some complex collection filtering / sorting (my events are quite complicated so entry queries are not suitable)
// This is where the infinite loop occurs. It's trying to augment nested entries.
return $events->toArray();
Here's my workaround:
// Replace toArray() with a custom parser
return $this->convertEntries($events);
public function convertEntries($events) {
$data = [];
foreach($events as $event) {
$item = $event->toEvaluatedAugmentedArray([
'id',
'title',
'url',
//..other fields I need for the view. I'm excluding my 'article' bard field as it contains references to other entries.
]);
// I have to use the bardText modifier to extract only text elements and prevent infinite looping on nested entries
$item['article'] = Statamic::modify($event->value('article'))->bardText()->fetch();
$data[] = $item;
}
return $data;
}
It would be great if toArray() or toEvaluatedAugmentedArray() excluded augmentation of nested entries by default in order to prevent infinite loops, OR, if there was an option / new function to skip augmentation of nested entries.
Thanks