You are here

public function Table::preRender in Views Aggregator Plus 8

Note that this class being a views_plugin, rather than a views_handler, it does not have a post_execute() function.

This function applies to the currently visible page only. If paging is enabled for this display view->result may only contain part of the entire result set.

Overrides StylePluginBase::preRender

File

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

Class

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

Namespace

Drupal\views_aggregator\Plugin\views\style

Code

public function preRender($results) {
  if (isset($this->view->is_temp_views_aggregator)) {
    return;
  }
  parent::preRender($results);
  if (empty($this->view->result)) {
    return;
  }
  $functions = $this
    ->collectAggregationFunctions();
  $show_global_totals_with_pager = empty($this->options['column_aggregation']['totals_per_page']) && !empty($this->view->total_rows);
  if ($show_global_totals_with_pager) {
    $this->view->is_temp_views_aggregator = TRUE;
    $args = $this->view->args;
    $display_id = $this->view->current_display;
    $view_displays = $this->view->displayHandlers
      ->getInstanceIds();
    if (in_array($display_id . '_no_pager', $view_displays)) {
      $clone = $this->view
        ->createDuplicate();
      $clone->is_temp_views_aggregator = TRUE;
      $clone
        ->executeDisplay($display_id . '_no_pager', $args);

      // First apply the row filters (if any), then aggregate the columns.
      // Only interested in column aggregation, so only 'column' group needed.
      $column_group = [
        'column' => [],
      ];
      foreach ($clone->result as $num => $row) {
        $column_group['column'][$num] = $row;
      }
      $totals = $clone->style_plugin
        ->executeAggregationFunctions($column_group, $functions);
      $clone
        ->postExecute();
      $clone
        ->destroy();
    }
  }

  // Because we are going to need the View results AFTER token replacement,
  // we render the result set here. This is NOT duplication of CPU time,
  // because self::renderFields(), if called for a second time, will do
  // nothing when self::$rendered_fields has been populated already.
  // render_fields() will puts currency signs in front of moneys, embeds node
  // and taxonomy term references in hyperlinks etc.
  $this
    ->renderFields($results);

  // Apply the row filters first, then aggregate the groups.
  $this
    ->applyRowFilters();
  $groups = $this
    ->aggregateGroups();
  $with_pager = FALSE;
  $values = $this
    ->executeAggregationFunctions($groups, $functions);
  unset($groups['column']);

  // Prepare for aggregation based on selected option.
  if (isset($this->options['group_aggregation']['group_aggregation_results'])) {
    $group_aggregation_results = $this->options['group_aggregation']['group_aggregation_results'];
  }
  else {
    $group_aggregation_results = 0;
  }

  // Normal aggregation selected - write the results and compress the groups.
  if ($group_aggregation_results == 0) {

    // Write group aggregation results into the View results.
    $this
      ->setAggregatedGroupValues($groups, $values, $group_aggregation_results);

    // With the aggregation functions now complete, destroy rows not part
    // of the aggregation.
    $this
      ->compressGroupedResults($groups);
  }

  // Sort the table based on the selected sort column, i.e. $this->active.
  if (isset($this->active)) {

    // To aid in sorting, add the row's index to each row object.
    foreach ($this->view->result as $num => $row) {
      $this->view->result[$num]->num = $num;
    }

    // First sort by default order.
    if ($this->options['default']) {

      // Store current sorting options.
      $sort = $this->active;
      $order = $this->order;

      // Set default sorting options.
      $this->active = $this->options['default'];
      $this->order = !empty($this->options['order']) ? $this->options['order'] : 'asc';
      $this->order = isset($this->options['info'][$this->active]['default_sort_order']) ? $this->options['info'][$this->active]['default_sort_order'] : $this->order;

      // Sort by default
      uasort($this->view->result, [
        $this,
        'compareResultRows',
      ]);

      // Restore current sorting options.
      $this->order = $order;
      $this->active = $sort;
    }

    // Fix counter fields.
    $this
      ->fixCounterFields();

    // Now sort by active order.
    uasort($this->view->result, [
      $this,
      'compareResultRows',
    ]);
  }
  else {

    // Fix counter fields anyways.
    $this
      ->fixCounterFields();
  }

  // Set the totals after eventual sorting has finished.
  if (empty($this->view->totals)) {

    // If not already set above, write the column aggregation result row on
    // the View object. This row will be rendered via
    // template_preprocess_views_aggregator_results_table().
    $this->view->totals = $this
      ->setTotalsRow($values);
  }

  // If we have a pager enabled with option set to show total
  // for whole resultset insteead of page - overwrite the
  // page totals as last step.
  if (isset($totals)) {
    $this->view->totals = $this
      ->setTotalsRow($totals);
  }

  // Aggregate the results per group and show them in a separate row,
  // without compression.
  if ($group_aggregation_results == 1) {
    $this
      ->setAggregatedGroupValues($groups, $values, $group_aggregation_results);
  }
}