Commit 98507de8 authored by Jordan Karlov's avatar Jordan Karlov Committed by Jordan Karlov
Browse files

Issue #3239548 by JordiK: Views pager ignores group aggregation

parent 02ea20b5
Loading
Loading
Loading
Loading
+34 −34
Original line number Diff line number Diff line
@@ -237,10 +237,10 @@ class Table extends ViewsTable {
      '#title' => $this->t('Column aggregation row applies to'),
      '#type' => 'radios',
      '#options' => [
        1 => $this->t('the page shown, if a pager is enabled'),
        0 => $this->t('the entire result set (CAUTION: To enable this, create a copy of this view display, disable the pager there, set the <i>machine name</i> to <i>DISPLAY_NAME_no_pager</i> (e.g. page_1_no_pager) and give it a different path.)'),
        0 => $this->t('the entire result set <em>CAUTION: This could cause performance issues with large tables!</em>'),
        1 => $this->t('the page shown (if pager enabled) or the result subset (if offset defined)'),
      ],
      '#description' => $this->t('If your view does not have a pager, then the two options are equivalent.'),
      '#description' => $this->t('If your view does not have a pager or offset, then the two options are equivalent (whole resultset).'),
      '#default_value' => $this->options['column_aggregation']['totals_per_page'],
      '#weight' => 1,
    ];
@@ -325,18 +325,25 @@ class Table extends ViewsTable {
      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) {
    $global_totals = $this->options['column_aggregation']['totals_per_page'];
    $pager = $this->view->display_handler->getOption('pager');
    $with_pager = ($pager['type'] != 'none' || ($pager['type'] == 'none' && $pager['options']['offset'] > 0)) ? TRUE : FALSE;
    $show_global_totals_with_pager = empty($global_totals) && $with_pager == TRUE;

    // If totals based on all rows and pager is set - get the whole resultset.
    if ($show_global_totals_with_pager && !empty($this->view->total_rows)) {
      $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);

      // Remove any paging and offsets and execute the display.
      $clone->setItemsPerPage(0);
      $clone->setOffset(0);
      $clone->executeDisplay($display_id, $args);

      // First apply the row filters (if any), then aggregate the columns.
      // Only interested in column aggregation, so only 'column' group needed.
@@ -348,7 +355,7 @@ class Table extends ViewsTable {
      $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
@@ -360,7 +367,6 @@ class Table extends ViewsTable {
    // 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']);

@@ -413,20 +419,14 @@ class Table extends ViewsTable {

    // 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.
      else {
        $this->view->totals = $this->setTotalsRow($values);
      }
    }
    // Aggregate group results and show them in a separate row, no compression.
    if ($group_aggregation_results == 1) {
      $this->setAggregatedGroupValues($groups, $values, $group_aggregation_results);
    }