In a Rules component I have a list of integer (as variable data type). I have to calculate the sum of items and to expose the result. Any suggestions?
-
I've been a big user of rules, so I could be wrong, but I don't think there are a whole lot of arithmetic support in general in rules. I believe you'd need to write your own rule for this. (Which could btw be an excellent addition to contrib)– LetharionSep 22, 2013 at 11:13
-
Hey @Letharion , if you are ever around on drupal.SE again, I'd be interested in your feedback/review of my answer to this question, which is about a field in a field collection (= variation of the list of integers in this question).– Pierre.VriensNov 22, 2016 at 15:04
-
1@Pierre.Vriens I'm honored you'd seek out my opinion specifically. :) Sadly I work very little with Drupal these days, and I fear I can't add much to your answer. It looks rather well written as it is. :)– LetharionNov 22, 2016 at 20:50
-
@Letharion : merci for your feedback ... anyway!– Pierre.VriensNov 23, 2016 at 12:25
2 Answers
I found the solution.
- Add a variable (integer) "total", for example.
Loop the list of integers:
- Calculate a value, input 1:
[total]
, input 2:[line-item]
, return: "result". - Set a data value, data:
[total]
, value:[result]
.
- Calculate a value, input 1:
- Outside of the loop, the
[total]
value is still available.
this is my suggestion, create the following function in your template file:
function <theme>_preprocess_node(&$variables)
{
}
and by kpr() function from devel module try to catch where these outputs are positioned in the page array. now you can take them by a foreach loop and cast them to integer.
-
1I don't believe that helps the OP, as the sum should be useful in a rule, not just for display. Sep 22, 2013 at 11:11