1

After doing some research, it seems like the best way to get a Total(s) row added to a Views table is via the Views Aggregator Plus module. (Is there an easier/better way?)

As can be seen below, I'm trying to list a bunch of nodes grouped by the first column (the taxonomy term "Type"), with the second column containing the number of each.

Views table

I'd like to add an additional row to the bottom:

Total | 6

...but no matter what I try, the additional row doesn't show up in spite of what the module documentation, various videos, and blog posts are telling me.

In the view Format, I'm using "Table with aggregation options", and the settings are as can be seen here:

Table with aggregation options settings

Under Column Aggregation, I should be able to check "Apply column function" and select Sum (I think), but this has no effect on the live preview. I tried this on both fields (and even both together), and it made no difference.

I'd like to make the Total row part of the table, rather than a hacky solution where I calculate it in the view header or footer. How can this be done? What am I missing?

0

I'd like to make the Total row part of the table, rather than a hacky solution where I calculate it in the view header or footer. How can this be done?

I've never used that module, but alternatively, you could do the template file solution.

Since you want this to work in the View's preview, which uses core's admin theme and since you should never hack core, this cannot be solved via theme. Therefore, must be solved via custom module.

Example:

  • View ID: content
  • View Display ID: page_1

/mymodule/mymodule.module

/*
 * Implements template_preprocess_views_view_table()
 */
function mymodule_preprocess_views_view_table(&$variables) {
  $view = $variables['view'];
  if ($view->id() == 'content' && $view->current_display == 'page_1') {
    $variables['total_rows'] = $view->total_rows; // pass custom var to template
  }
}

/*
 * Implements hook_theme()
 */
function mymodule_theme() {
  return array(
    'views_view_table__content__page_1' => [
      'template' => 'views-view-table--content--page-1',
      'base hook' => 'view',
    ],
  );
}

/mymodule/templates/views-view-table--content--page-1.html.twig

Copy the code of views-view-table.html.twig and paste into this file and just change the last 3 lines to:

  {% endfor %}
  <tr>
    <td>{{ 'Total'|t ~ ' | ' ~ total_rows }}</td>
  </tr>
  </tbody>
</table>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.