You are here

protected function Table::compareResultRows in Views Aggregator Plus 8

Compare function for aggregated groups, for use in sorting functions.

Parameters

\Drupal\views\ResultRow $row1: The first aggregated group of result rows.

\Drupal\views\ResultRow $row2: The second aggregated group of result rows.

Return value

int The compare code indicating whether $row1 is smaller than (-1), equal to (0) or greater than (1) $row2.

File

src/Plugin/views/style/Table.php, line 1370

Class

Table
Style plugin to render each item as a row in a table.

Namespace

Drupal\views_aggregator\Plugin\views\style

Code

protected function compareResultRows(ResultRow $row1, ResultRow $row2) {

  // The sorting data may be raw or rendered, while the sorting style may be
  // alphabetical or numeric.
  //
  // Columns that need to be sorted using raw values:
  // o numbers and money, so that "$1,000" comes AFTER "$9.99" (ascending)
  // o dates and date ranges
  //
  // Columns that need to be sorted using rendered, post-aggregated values:
  // o Custom: Text field, Views Field View, addresses, taxonomy terms.
  // When no default column sort is set, $this->active will equal -1.
  if (empty($this->active) || $this->active == -1) {
    return 0;
  }
  $field_handler = $this->view->field[$this->active];
  if (isset($field_handler->options['entity_field'])) {
    $field_name = $field_handler->options['entity_field'];
  }
  else {
    $field_name = $field_handler->options['id'];
  }
  $plugin_id = $field_handler
    ->getPluginId();

  // AddressFields, taxonomy terms and custom text fields,
  // views and webform fields are compared in rendered format.
  $compare_rendered = in_array($plugin_id, [
    'addressfield',
    'taxonomy_term_reference',
    'custom',
    'view',
    'webform_submission_field_numeric',
    'webform_submission_field',
  ]);

  // Get the cells from rendered fields.
  if ($compare_rendered) {

    // Special handling of "Webform submission data".
    if ($plugin_id === 'webform_submission_field_numeric' || $plugin_id === 'webform_submission_field') {
      $field_name = $field_handler->definition['webform_submission_field'];
      $data1 = $field_handler->view->result[$row1->num]->_entity
        ->getData();
      $cell1 = $data1[$field_name];
      $data2 = $field_handler->view->result[$row2->num]->_entity
        ->getData();
      $cell2 = $data2[$field_name];
    }
    else {
      $cell1 = trim((string) strip_tags($this
        ->getField($row1->num, $field_name)));
      $cell2 = trim((string) strip_tags($this
        ->getField($row2->num, $field_name)));
    }
  }
  else {
    $cell1 = $this
      ->getFieldValue($row1->num, $field_name);
    $cell2 = $this
      ->getFieldValue($row2->num, $field_name);
  }
  if ((double) $cell1 == (double) $cell2) {

    // If both cells cast to zero, then compare alphabetically.
    $compare = $cell1 == $cell2 ? 0 : ($cell1 < $cell2 ? -1 : 1);
  }
  else {

    // Compare numerically, i.e. "20 km" comes after "9.5 km".
    // The double cast causes a read up to the first non-number related char.
    $compare = (double) $cell1 < (double) $cell2 ? -1 : 1;
  }
  return $this->order == 'asc' ? $compare : -$compare;
}