Hi - I suspect that I might be doing this all wrong, so any help would be appreciated.
I have been able to add my own View PHP column to a view with the following in the Output code field:
<?php
$nid = $data->nid;
if ($data->_field_data['nid']['entity']->field_featured['und'][0]['value'] == 1) {
echo '<a href="node/' . $nid . '">' . $data->_field_data['nid']['entity']->field_project['und'][0]['value'] . '</a>';
} else {
echo $data->_field_data['nid']['entity']->field_project['und'][0]['value'];
};
?>
What this is doing is checking if a condition is true and if so wrapping a field in an href. This work nicely.
What I now want is to be able to make this field "sortable". The view is set up as a table and I discovered that in order to make this column sortable I needed to check the 'Enable click sort' (see Enable click sort screenshot). Once I selected a value here I was then able to check the sortable option in the table settings (see Table column sort screenshot).
This does indeed make the column sortable but its not sorting how I would imagine (in fact I haven't yet worked out how it is sorting).
See Before Sort screenshot - this shows the first text column in red where the condition is true. The After Sort screenshot shows the result of clicking on the column header (Project) but is not sorted alphabetically.
I have tried stripping out the condition and just echoing the field, thinking that the href might be messing it up. I have tried the option to 'Sort using custom php code' with the following in the Click sort code field:
return $data->_field_data['nid']['entity']->field_project['und'][0]['value'];
But neither work so far. What am I doing wrong please?
Thanks
Tim
Comment | File | Size | Author |
---|---|---|---|
After Sort.jpg | 110.89 KB | timtunbridge | |
Before sort.jpg | 110.02 KB | timtunbridge | |
Table column sort.jpg | 57.9 KB | timtunbridge | |
Enable click sort.jpg | 43.58 KB | timtunbridge |
Comments
Comment #1
ibandyop CreditAttribution: ibandyop commentedHave you found a work around yet? I would like to know as well.
I have not got any of the sorts to working as expected. But I have two issues which shed more light on how to use this feature.
First
I have this error when I place php code in the sort area. I would like to know how to get rid of it.
Second
Any help , example or documentation greatly appreciated.
Comment #2
gardy CreditAttribution: gardy commentedHi.
I think that I found something about this.
In views_php_handler_field.sort.inc @ line 75 in the function called php_post_execute, the usort called with wrong arguments.
As the php manual says there should be a & mark because the usort wants only the reference of the array. Like in the manual's example: usort ( array &$array , callback $cmp_function ).
But I thinks there is something wrong with the php_sort helper function, because the result is still wrong.
I'll keep finding a solution...
Comment #3
HenrikBak CreditAttribution: HenrikBak commentedsubscribing
Comment #4
fuerst CreditAttribution: fuerst commentedJust a hint: You have to compare the former row (
$row1->php
) with the current row ($row2->php
). Result must be$row1->php
should be above$row2->php
$row1->php
should be below$row2->php
For instance to sort alphabetically you may use that code:
Comment #5
dankoB CreditAttribution: dankoB commentedStill not working for me. Could it be related to the other problem where it doesn't see the values of fields in a $row as discussed here http://drupal.org/node/1140896? I finally got beyond the problem of displaying the right value by putting all the php in the Output box as follows:
Is it unable to sort correctly since it's not seeing the row field values properly?
Comment #6
stinky CreditAttribution: stinky commentedSubscribe. Anyone find an answer yet?
Comment #6.0
stinky CreditAttribution: stinky commentedclarification