On Drupal 8 site, I have a exact copy of the below excel example table made with views but without the yellow cell yet.
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']
];
}
}