mmikkel / CpSortableCustomColumns-Craft

Adds custom fields to sortable attributes in element indexes
41 stars 5 forks source link

Allow sorting on BaseElementFieldType #7

Open timkelty opened 8 years ago

timkelty commented 8 years ago

Not sure how feasible, but it would be great if element field types were allowed for sorting. In this case, I would want it to sort on the title of the first element in the criteria (which is what displays).

Especially helpful if your entries/element field is limiting to 1 selection. screen shot 2016-06-17 at 10 29 54 am

mmikkel commented 8 years ago

Thanks for contributing, @timkelty!

I concur, sorting on relational fieldtypes would be really useful. It's easier said than done, though – you'd both have to override the call to elementIndex/getElements somehow (either by writing a custom element index, like Zenbu does, or just override the action method somehow) and I guess probably write custom database queries aswell, as the ElementCriteriaModel doesn't support sorting on relational data out of the box.

Another possible approach to consider, is the fact that the ElementCriteriaModel's order attribute does accept MySQL functions (see http://craftcms.stackexchange.com/questions/51/how-do-you-sort-elements-queried-from-multiple-channels for an example). I'm not totally sure how to achieve relational sorting using this approach, though – keep in mind that all relations in Craft are stored in a separate table (craft_relations) from the basic fieldtypes/attributes (craft_content), i.e. the sortable ones.

I'll keep the issue open as an enhancement/feature request, but to be honest its not very probable that I'll ever get around to it :)

As a workaround to the whole issue, I'd suggest you take a look at the Preparse plugin. You could create a hidden Preparse field that outputs an alphabetical, comma separated list of the titles for all the elements in your element field, and then add that field to the CEI instead of the "real" relational field – which would enable you to sort on the value (which is just a string). Of course, there are tradeoffs, but its the best workaround I can think of at present.

timkelty commented 8 years ago

Thanks for the reply! I figured it wasn't implemented for a reason. Preparse is a good option if I absolutely need this.

tommymarshall commented 7 years ago

This would be great, we're looking for this functionality as well.