Drupal 7 Views Calc Module

By shane
Sat, 2013-02-23 11:19
6 comments
Daily Dose of Drupal Episode #114

Share with Others

The Drupal 7 Views Calc module provides an easy way to do calculations on the numeric data that is displayed within a Drupal 7 View.

In this episode you will learn:

  • How to create a Drupal 7 Views Calc view
  • How to use Views Calc to perform calculations such as SUM and AVG on a Drupal 7 field within a View

Thanks to Drupalize.me for sponsoring this episode of the Daily Dose of Drupal.

If you are new to views, you can check out our Drupal views introduction.

Hello everyone and welcome to another Daily Dose of Drupal, today we’re on Episode Number 114 and we’re going to be going over the Views Calc Module. The views Calc Module allows you to easily use sums, averages, counts, mins or maxs within a View.

Basically it allows you to get a lot of nice calculations about that data that is actually being displayed inside a Views Table so as you can see in this example it allows counting, summing and maxing of the various data that’s shown in this view. But before we get started; as always I’m Shane Thomas, you can follow me on Twitter at smthomas3, you can also go to codekarate.com and sign up for the newsletter.
If you haven’t checked out drupalize.me please do so. Drupalize.me is one of the best ways to learn about Drupal. You can simply come here to browse, see some of the newest videos and you’ll notice that not only do they have a huge selection of videos.
They have a lot of videos here for free so go ahead and go to drupalize.me and start learning more about Drupal. If you do decide to sign up you can use the coupon code CK20FEB and you get 20% off and they know that I sent you over there. Let’s go ahead and get started; so I have a test site here and I already have the Views Calc Module installed so I’ll go ahead and show that.

As you can see down here the Views Calc Module has been installed. I’m using 7.x-1.0 and the Views Calc Module really has two major features that you can use it for. I’m only going to go over one of those today; the first is that you can create custom fields that can be used inside … the actual inside of your View as just another field.

I’m not going to go over that because there’s not really a lot of uses that I’ve seen for that, there can be a few but it’s not as important as being able to actually do the other types of calculations. You can change the settings for allowable functions in operators; you can also export and import your Views Calc fields so you can move it across other websites.

So if you need to move from one Drupal site to another you can easily reuse your Views Calc custom fields. What I’m going to do is I’m actually going to create a new View called Time Entry but before I do that I’m going to show you my content type that I created. I created a content type called Time Entry which if I go to the manage fields you’ll see it’s very simple. It has a title, it has a date value using the date module and it has a decimal value for time log to … think of it as a simple way for people or just simple way to display logged time and just says uses and example so we can show you how this View could work.

So I’ll create my View called Time Entry, I’m going to show content of type Time Entry and I’m going to create a page here called Time Entry. The display format … this is the important part here; I need to select the Views Calc Table, you can select how many items I’m going to display, I’ll go ahead and display all of them and now I click continue and edit.

The first thing I’m going to do is add my fields. Remember I have a date field which I’m going to add and I also have a Time Log field so I’ll add that field as well and click apply All Displays, I’ll go ahead and keep both of these at the defaults. I’m also going to go ahead and remove this title field because it’s not really necessary for this example.
As you can see if I scroll down I have the date and the time log showing up here in the preview. What I really want to do is I want to see the actual sum of this time so I simply click on the settings here; as you can see it was right on the Views Calc table format, click settings and you can actually display the calculation for this time log field. I can do things like sum, count, average, minimum.

I’m going to go ahead and do sum and average to show a couple examples and I click Apply All Displays and it automatically adds those sections to the bottom. So here it sums up the value of all these fields and also shows me my average.

So that’s really all there is to Views Calc, it’s a pretty simple module if you just want to display minimums, maximums, counts, sums or averages in basically like a total section on the bottom of your Views Table. We’ll go ahead and look at the full page just to see how it looks, I need to go ahead and make sure I save my View of course and as you can see it gives you the nice little table with the dates and the time log.

You can do a whole bunch of things like add, expose filters here so you can select different date ranges and it’ll change the actual sum and average time based on the exposed filters value so it’s really flexible, really useful if you’re doing or displaying data that has numeric values that you may need to do calculations on. So go ahead and give Views Calc a try and the next project you need to perform these types of calculations and we want to thank drupalize.me again for sponsoring this episode of the Daily Dose of Drupal.

If you’d like to become a sponsor go ahead and go to codekarate.com and click on the code karate sponsorship page and learn more. I want to thank you for watching the Daily Dose of Drupal and we’ll see you next time.

Comments

Hi,

thanks for this episode of DDoD, enjoyed it.

Well, how can I get rid of the PHP Notices, when displaying the view?!

And what module do you use for the "Back To Top" Button? I like the design ;-)

Regards,

Tobi

You can hide error messages from the screen by going to Configuration -> Development -> Logging and errors. You can also change your PHP settings to not show PHP notices. Something like this in your php.ini settings file might do it:

error_reporting(E_ALL ^ E_NOTICE);

The other and most likely better option would be to try to figure out what is causing the issues and fix them. If you go that route you can provide a patch on Drupal.org to help others :-)

As far as the back to top button, I go over two modules in this module comparison episode

Thank you for another great video post....
What module filter are you using in this videos? It's not the module_filter.

Hi Shane,

thank you for this tutorial. Just to point me in the right directions since I am fairly new to Drupal a quick question please.

I want to use the calculated values results of views-calc table e.g. average of a 'user-points' field to perform an action on the page. For example: If calculated field is of average: 4 send user an email with a specific content or propose an event to him etc.

Would it be an adequate way to use rules for that or do you know any specific module which triggers certain actions based on these calculated values?

Thank you for answer.

I don't think there is an easy way to do this with Rules. With Rules you need an event to happen, what would the event be in your case? Would it need to run every time someone visited the page?

I think the only way to do this is to write a custom module that runs something on periodically (on the cron run). You would need to run a custom query to calculate the average and depending on the result, send an email or perform other actions.

I am not aware of a way to make rules work for what you are looking to do.

Good luck and hopefully you figure out a solution.

Post new comment