You are here

function ViewsPhp::phpSort in Views PHP 8

Helper function; usort() callback for sort support.

File

src/Plugin/views/sort/ViewsPhp.php, line 98
Definition of Drupal\views_php\Plugin\views\sort\ViewsPhp.

Class

ViewsPhp
A handler to sort a view using PHP defined by the administrator.

Namespace

Drupal\views_php\Plugin\views\sort

Code

function phpSort($row1, $row2) {
  $factor = strtoupper($this->options['order']) == 'ASC' ? 1 : -1;
  $function = $this->php_sort_function;
  foreach (array(
    'row1' => 'normalized_row1',
    'row2' => 'normalized_row2',
  ) as $name => $normalized_name) {
    ${$normalized_name} = new ViewsPhpNormalizedRow();
    foreach ($this->view->display_handler
      ->getHandlers('field') as $field => $handler) {
      ${$normalized_name}->{$field} = isset(${$name}->{$handler->field_alias}) ? ${$name}->{$handler->field_alias} : NULL;
    }
  }
  $result = (int) $function($this->view, $this, $this->php_static_variable, $normalized_row1, $normalized_row2);
  return $factor * $result;
}