Script-based sorting expression in content queries

Imagine the following scenario:
You have a list of content that is to be sorted by its title on a webpage. But the title may not always be a specific value in the contenttype. It could be a new value made by some logic in the controller, or it could be an optional value with fallback in the view. Often this scenario can be solved by first doing the query, and then later in your controller do the actual sorting of your content. But if you need to fetch content in batches (pagination or similar), that method will produce the wrong result.

In other words, I’d like to be able to sort in my content query based on some kind of virtual or hybrid field. I’d imagine the most common use case would be optional values with fallback, but it could also be on dates or numbers that are to be processed before showing them to the end user.

4 Likes

Two solutions from the top of my mind:

  1. Virtual fields: Possible to define values in a content-type that uses a provided script to populate its value. This script could then gather the needed values into a order-field. A virtual field like this will also have multiple other usages

  2. Query-function accepting two values for sorting, one primary and one to be used if the first one does not exist / null. This would then have to be translated into a scripted sort-value behind the scenes. This soultion seems a bit more tricky and has to be investigated a bit more before.

2 Likes