I came across a blog post the referenced another forums post on how to display a sum total for the currency values displayed in a listview result. This is a pretty common use case I would imagine, as often times you would want to do these summations to avoid pulling out the calculator if you are doing forecasting by hand for example with the opportunities module. Nonetheless, reading the customization details on how to do this seemed quite involved ( and rather upgrade-unsafe ), so I thought I would dig in for a simpler way to do this.
The crux of what was needed is this:
- A simple way to sum up all the values in the column(s) of our choosing ( in this case Opportunities )
- A way to interject this display into the template
We have two logic hooks which are quite adept at doing this. First is process_record, which is a hook called for each bean record listed on a ListView. The second is after_ui_frame, which is called after the main content display is done, as is a nice way to add in additional content to a view.
Let’s smack these together in a single logic hook class, as shown below.
How this works, is for each record processed, we increment a static counter in the class ( doing this gets around the case where the Logic Hooks functionality creates new instances of the class ). Then at the end with the after_ui_frame hook, use jQuery to stick the total count exactly where we want it in the display ( between the module title and the search form itself ).
The best part? Completely upgrade-safe and doesn’t touch anything with core files. Worst case, you just need to adjust the jQuery positioning code to put things where you like on the form.