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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ibandyop’s picture

Have 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.

Warning: usort() [function.usort]: Array was modified by the user comparison function in views_php_handler_field->php_post_execute() 

Second

  • I believe part of the problem is that sorting uses different available variables than output code for example $data is missing
  • Sorting also uses php usort() to compare row1 with row2., I unable to create a suitable function that can be passed to the usort function defined in views_php_handler_field
  • It would be great if someone showed us how to test the sorting function using the available variables available in the sort text area box

Any help , example or documentation greatly appreciated.

gardy’s picture

Hi.

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...

HenrikBak’s picture

subscribing

fuerst’s picture

Just a hint: You have to compare the former row ($row1->php) with the current row ($row2->php). Result must be

  • a negative value if $row1->php should be above $row2->php
  • a positive value if $row1->php should be below $row2->php
  • 0 if no sorting necessary

For instance to sort alphabetically you may use that code:

return strnatcasecmp($row1->php, $row2->php);
dankoB’s picture

Still 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:

$currentschlyear = (date ('Y', (strtotime('now + 5 months'))));
$gradelevel = 12 - ($data->field_field_high_school_grad_year[0]['raw']['value'] - $currentschlyear);
print $gradelevel;

Is it unable to sort correctly since it's not seeing the row field values properly?

stinky’s picture

Subscribe. Anyone find an answer yet?

stinky’s picture

Issue summary: View changes

clarification