Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
What I have:
A php custom field that returns directly a number (2, 3, 0, 1 ...).
What I want: sort by that field.
But I don't know exactly how to, whatever I do in the sort php field doesn't change the overall result.
Thanks in advance.
Kind regards.
Comments
Comment #1
infojunkie CreditAttribution: infojunkie commentedIn the Sort code section of the Sort field, you need to implement a comparison function for your custom field. The code should look something like this:
Basically, the function should return -1, 0, 1 depending on whether the field value in
$row1
is less than, equal, or greater than the value in$row2
.To find the name of your custom field, you can prepend something like
dsm($row1);
to the code above (with Devel module enabled).Comment #2
thedavidmeister CreditAttribution: thedavidmeister commentedthis is actually an invaluable little nugget of an explanation, although IMO using the ternary operator with nested comparisons makes the example unnecessarily hard to follow when you're aiming to make things *more* obvious for early adopters :P
below is how i got my gig guide, that used UNIX timestamps, to sort using only "day granularity" (since i was grouping the display by day) and then sort each day alphabetically by node title.
NOTE: without views_php, i had no way of changing the granularity of my date fields in the sort. Thanks for the great module!
EDIT: did some further testing, this doesn't quite do what i expected.. will get back to you with more refined code when i figure out how this is all supposed to work.
Comment #3
thedavidmeister CreditAttribution: thedavidmeister commentedah, i totally munged the way arguments should be passed to strtotime.
this actually works:
Comment #4
infojunkie CreditAttribution: infojunkie commented@thedavidmeister, thanks for the expanded explanation. Marking as fixed.
Comment #5
thedavidmeister CreditAttribution: thedavidmeister commentedwell it is only half fixed really.. there's still no README.txt file provided with the project.
i'll open a separate issue for that though.
Comment #6
grasmash CreditAttribution: grasmash commentedSo it seems that if you use a Views PHP field as a sort handler, subsequent (views-generated) sort handlers do not work correctly? I'm assuming that's why you had to run strcmp() rather than simply adding a normal views title sort handler.
Is this true? If so, it needs to be noted in the help text for the sort handler.
Comment #7
Liam MorlandDrupal 6 is no longer supported. If this applies to a later version, please re-open and update the version.