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) – Letharion Sep 22 '13 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.Vriens Nov 22 '16 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. :) – Letharion Nov 22 '16 at 20:50
  • @Letharion : merci for your feedback ... anyway! – Pierre.Vriens Nov 23 '16 at 12:25

I found the solution.

  1. Add a variable (integer) "total", for example.
  2. 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].
  3. Outside of the loop, the [total] value is still available.
| improve this answer | |

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.

| improve this answer | |
  • 1
    I don't believe that helps the OP, as the sum should be useful in a rule, not just for display. – Letharion Sep 22 '13 at 11:11

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.