0

On Drupal 8 site, I have a exact copy of the below excel example table made with views but without the yellow cell yet.

View table with calculation

The view is displaying one content type: Client where all the below fields are available:

  • Date
  • Client Name
  • Client ID
  • Amount ($)

Now, as you can see, the yellow cell is displaying the total SUM of all amount ($) rows. which basically is: 15+29+97+115+45+67+23+11+5+91+44 = 542$

How can i achieve this exact calculation in my view ?

I have tried to enable the Use aggregation option under the advanced section on my view, but it seems it doesn't do what i am asking for!

any help please ?

Note: I believe the use Use aggregation option doesn't add a row at the bottom of the table calculating the sum of all above rows and therefore this question Making a colums total (SUM) with Views is not a possible duplicate of this one.

UPDATE #1

After following the answer of @Stef Van Looveren, I am encountering a new error:

InvalidArgumentException: Field field_money_amount is unknown. in Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (line 587 of /home/####/public_html/#####/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php)

Knowing that: field_money_amount is the id of the field taken from the content type being used inside the view.

Complete code in the .module file:

<?php

use Drupal\views\ViewExecutable;

function my_module_views_pre_render(ViewExecutable $view){
  if ($view->id() == "view_id" && $view->current_display == 'display_id') {
    $total = 0;
    foreach ($view->result as $value) {
      $number = $value->_entity->get('field_money_amount')->value;
      $total += $number;
    }
    $view->attachment_after = [
      '#markup' => '<table class="table cols-0"><tr><td>Total:</td><td>'.$total.'</td></tr></table>',
      '#allowed_tags' => ['table','tr','td']
    ];
  }

}

  • 2
    Possible duplicate of Making a colums total (SUM) with Views – Jdrupal Sep 11 at 8:44
  • @jdrupal It is not a duplicate at all, please check my above answer, I am obviously asking on how to add a row at the bottom of the table calculating the sum of all above rows, the referred question does not explain that. – user94449 Sep 12 at 7:33
0

Interesting question and a typical use case. In a custom module, do the following:

use Drupal\views\ViewExecutable;

function helper_views_pre_render(ViewExecutable $view){
  if ($view->id() == "numbers" && $view->current_display == 'page_1') {
    $total = 0;
    foreach ($view->result as $value) {
      $number = $value->_entity->get('field_number')->value;
      $total += $number;
    }
    $view->attachment_after = [
      '#markup' => '<table class="table cols-0"><tr><td>Total:</td><td>'.$total.'</td></tr></table>',
      '#allowed_tags' => ['table','tr','td']
    ];
  }
}

Change the names according to your view name & field names. I'm sure you get the point: you add a table below with the total result.

I did a test and it gave me the following result:

enter image description here

  • thank you for your reply, two simole question: (1) does the function helper_views_pre_render(ViewExecutable $view) will trigger everytime the view is being visited ? (2) I have more than 500 rows results in the view, so do you think foreach will affect the performance in a way or another ? – user94449 Sep 12 at 18:34
  • Dear @Stef Van Looveren, I have: (1) Added your above code to the .module file of my custom module. (2) Change the View Name, Display Name & Field names and (3) Clear all caches... but still when i visit the view, nothing appeared at the bottom of the table. any ideas why ? – user94449 Sep 16 at 9:23
  • Hi Aka, 1) Is the module enabled? 2) did you change helper_views_pre_render to mymodulename_views_pre_render, ...? – Stef Van Looveren Sep 16 at 9:25
  • Dear @Stef Van Looveren, my mistake, i forget to change helper_views_pre_render to mymodulename_views_pre_render but can you please check the UPDATE #1 of my question for a new error now available !!! – user94449 Sep 16 at 18:34
0

After further search, I have found Views Aggregator module to do exactly what I need to do.

I am willing to accept @Stef Van Looveren answer especially where i prefer not to have to add a new module to my site but for some reason isn't working for me, unless somebody can help fix the above provided code.

Your Answer

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