Feature Request: Natural Sort
le_petit_basil - November 18, 2008 - 19:21
| Project: | Views Custom Field |
| Version: | 6.x-1.x-dev |
| Component: | User interface |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Description
Views Custom Field Rocks. But it would be even better if it were possible to sort on Custom Fields.

#1
It uses PHP afterwards to generate row-specific text so its currently not possible.
Could you give some examples how you like to use it? Sort on what?
#2
For example have values row: 8 / 2, 10 / 5, 8 / 7, 3.4 / 2, 8 / 6 (before '/' - average fivestar value, after - voting user count), you need to sort this value as a string:
10/58/7
8/6
8/2
3.4/2
To do this, I glued them into one customfield field, but only now noticed that sort of pressing header in the table does not.
In views theming filles (\modules\views\theme\) I have full access to all views fields table, and just merge (or do other activities with them) they is not difficult without customfield :
<td class="views-field views-field-voting_value"><?=((!empty($row[value]) && is_numeric($row[value])) ? (round($row[value]/10, 2)) : ' ')?>
<?=(!empty($row[value_1]) ? '/' . $row[value_1] : ' ')?>
</td>
#3
You already can do this. Just add two sorts to your view; first on "average fivestar value" second on "voting user count".
Let me know if that's what you're looking for.
#4
Yes, ask if the criteria for grading in block vies, all appear as you want, but when you click on the header field tables default sorting break and starts sorting the field corresponding pressed headline, that is why I combine two fields (value_average and value_count) in one
#5
I could introduce a new customfield "fieldcombination" which combines two or more fields in the query; then it would be possible to sort on those fields.
But currently I don't have much time; I'll look into it next week.
#6
Sorry don't have time for this on the moment.
#7
I'm using CustomField to calculate some user info, which results in a single or double digit number (e.g 1 or 3 or 24) - how can i get views to enable sorting by this results?
#8
You can't, as customfields are evaluated after querying of the (sorted) result.
I have however some ideas to introduce some fields that allows to add calculations (or other sql functions) to a views query. Unfortunatly I don't have very much time right now; so it can take some time before this is implemented.
#9
Hey casey.
First of all: Thanks for your great module.
Reading this, i get the feeling this isn't gonna happen too soon, which i totally understand. It's a rather critical feature for me (and possibly many others). So it would be really nice if you could share your aforementioned ideas with us. Maybe someone who really needs this too and who is an experienced drupal developer is able to implement your ideas. And maybe, if i feel like it's easy enough for me, I will even try this myself (even though i'm very low skill on php and new to drupal development).
thanx in advance
#10
I use this module all the time, thanks for a great contribution.
Attached is a patch to provide sorting for PHP Code custom fields. I'd be happy if people here could use it and validate it, and if the module maintainer would consider committing it in. Since I need this for my professional (commercial) work, I'm willing to provide support for it. If it works well, I can extend this to Row Number field as well (I guess it doesn't make sense for Markup).
To activate sorting, just choose a sorting method in the field's settings. Make sure the fields in the Table style settings are marked as sortable.
#11
@kratib For about a month I don't have time to work on my drupal modules. I could give you CVS access?
#12
@casey thanks for our offer. Go ahead and grant me CVS access. In any case I will wait to hear from people who tried the patch before committing it.
Did you get a chance to try the patch?
#13
CVS access granted.
No, don't have time for anything at all (working on a paper for my study with a short deadline and after that a couple of exams).
I was actually also working on a solution for this. My idea was to allow the usage of views sort handlers as clicksorts (configurable per custom field). Don't have it fully working yet though.
#14
Right now my patch is just for the field handler to be click sortable. I don't know what it takes to write a sort handler, so I'll wait for people to tell me which one they need.
#15
@kratib: Tested the patch and works perfectly fine so far for me! Thank you so much!!! Great job!
@casey: Thank you for giving kratib cvs access. That's true open source spirit! And also thanx for checking your issue queue despite your school stress!
#16
I just committed the above patch (tweaked for performance) to the latest dev release. I tested it on a view with 10K nodes so things look alright to me. Note that this applies to PHP custom field only, not Rownumber yet.
I'll keep this active until I commit a similar patch for Rownumber, which will probably require refactoring some code to a common ancestor of these two classes to avoid code duplication.
#17
FYI: I just found a bug that happens when the PHP code generates HTML to be rendered. I committed a fix to the latest dev.
#18
Well, this last commit would be great for me if it would work. I've tried with all sorting types and nothing.
I'm trying to show the user's first + last name, eg:
<?php
echo '<a href="profile/' .
strtolower(str_replace(" ", "-",
$data->node_data_field_location_field_name_value . "_"
. $data->node_data_field_location_field_surname_value)) . '"'
. ' rel="lightframe[|width:670px; height:490px;]">'
. $data->node_data_field_location_field_name_value . ", "
. $data->node_data_field_location_field_surname_value
. '</a>';
?>
(tried also with simpler versions)
The output is correct, but I just can't sort... The column title doesn't appear as a link..
Any help appreciated.
#19
If you're using table output, then you need to open the table settings and set this field as sortable. Does that apply?
#20
Oh yes! Forgot that... Thank you for the quick answer!
Great job.
#21
@kratib: Could you please add a note in the description of the sorting option to describe a little that sorting will load all records of the query in memory to resolve the sort? Please. On my installation there is staff that can create views and I have given this module to them, but they may try to against thousands of records.
I would even ask for an option to disable sorting for PHP fields, or maybe if there was a PHP field sortable, and another one that is not sortable, then we could use the later so that users cannot take the site down when creating and then running a view against a huge number of records.
Other than that, nicely done. ;-)
Thanks
#22
@markus_petrux: I added the warning note as a description to the field. It is easy to set the sorting option to none (which it is by default). Also, sorting in memory won't occur until the header of the custom field is actually clicked.
#23
Thank you. This will save me one or two headaches.
#24
hi.... thanks for the patch... it works on table outputs.... sadly i use de grid output.. and don't works.... u have to do something special to this??
#25
The implemented feature is only for tables so far. To make it work for other styles, one has to implement sort handlers which is still not done. You can open a new feature request for it if you wish.