We've retired the forum in favor of GitHub Discussions.

New conversations can be started on GitHub. Existing conversations will remain for a while longer.

Head over to GitHub →

Statamic 3 - Using collection tag with custom sort modifier or ViewModel?

Chris Bachmeier July 19, 2020 by Chris Bachmeier

Hi all, Is it possible to apply custom modifiers to collections? Looks like those are all ignored when I tried it out... Or is there a different way that I'm missing?

Alternatively I was thinking to use a ViewModel where I use a query to get the entries, eg.

The entries have a radio fieldtype and depending on the selection additional fieldtypes.

Collection::find('myCollection')
  ->queryEntries()
  ->where('published', true)
  ->orderBy('from', 'asc') // initial order by common field
  ->get();

Then do some stuff with those entries, re-ordering depending on the additional fieldtypes and so on... finally I have an array that could look something like this one:

$newArray = [
  'optionX' => [ ... entries ordered by x ...],
  'optionY' => [ ... entries ordered by y ...],
  'optionZ' => [ ... entries  ordered by z...]
];

Now, how to convert the $newArray and pass it back to the view?

return [
  'newArray' => $newArray // augment/cascade? new array so it works the usual way in template?
]

Any suggestions are much appreciated : ) Thanks

Answered by Chris Bachmeier!
>>>>>>> Answered <<<<<<<
3 Replies
3 Followers