I have a view that is showing a node teaser. In this view I have a single Views PHP sort block that utilizes $row1 and $row2. The values of $row1 and $row2 are invalid.

I have another another view that is showing fields. The values of $row1 and $row2 appear correct.

Looking at php_sort() in views_php_handler_sort.inc, I see that the raw rows from the db $row1 and $row2 are "normalized".

It looks like this normalization assumes that the output of the view will be fields?

foreach (array('row1' => 'normalized_row1', 'row2' => 'normalized_row2') as $name => $normalized_name) {
      $$normalized_name = new stdClass;
      foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
        $$normalized_name->$field = isset($$name->{$handler->field_alias}) ? $$name->{$handler->field_alias} : NULL;

What I will do to hack around this issue is simply pass the original $row1 and $row2 (that get passed into the php_sort function) into $this->php_sort_function as 2 extra parameters on the end. That way I can have access to both the "normalized" $row1 and $row2 and the original $row1 and $row2.

Not sure what an actual solution would look like.


Erik.Johansson’s picture

I'm facing the same issue. Did you find any nice workaround for this?

goldenboy’s picture

Issue summary: View changes
Status: Active » Needs review
3.38 KB

I have the same problem. In my case I'm dealing with search_api index view. With this kind of view when normalized rows are passed through the lambda function are useless. With a search index view the actual form of the $view->result array is the following:

  0 => object(
    'entity' => [nid],
    '_entity_properties => array(...)

The 'normalisation' process, as you correctly exposed in the issue description, produces invalid rows because it cannot find any field_alias into the result array.

So, I wrote this patch that leave at the view's administrators the choice to use or not the normalisation process. Basically I added an option to the sort handler (use_normalized_rows, default=TRUE) and if the view's admin disable the option the normalisation process is skipped.

rimu’s picture

This patch works for me. The only thing I changed was

if (!empty($this->options['use_normalized_rows']) && $this->options['use_normalized_rows']) {

instead of

if ($this->options['use_normalized_rows']) {