Webform Calculation Number example
Webform Calculation Number settings
Webform Calculation Hidden example
Webform Calculation Hidden settings 1.x
Webform Calculation Hidden settings 2.x

Webform Calculation Components

This module provides AJAX functionality to perform basic arithmetic operations on webform components.

With this module the user can have calculated fields, e.g. the total amount, calculated percentage etc.

Note: a webform component is a field template used to create webform fields. Examples of webform components (provided by default) are textfield, e-mail, number, select etc. E.g. you can create a webform field called "First name" using the textfield component. This module provides two extra components:

Webform Calculation Number (Calculation - number)

That component is a numeric field where the user can type a number. The target field (result field) is set when configuring the Webform Calculation Number component.
When the webform is displayed, the user can enter numbers in these fields and the calculation is performed against the result field.

Webform Calculation Hidden (Calculation - hidden)

It can be used to perform operations with existing numeric components. The calculation is triggered when the user chooses a specific option in checkboxes, radio button or select option.
All that is configured when creating the Webform calculation hidden component.
It is possible to perform one operation e.g. Addition to several numeric fields, then the result will be calculated against the result number component.
Note that select field choices must be selected, (they can be more than one) and they have to correspond to the select field that triggers the calculation.

Available operations are:

[addition]
[subtraction]
[multiplication]
[division]
[percentage]
[modulo]

Configuration

For Webform Calculation Number

Go to add content > webform (or any webform content type):

  • Create a new webform
  • Create new webform components, you need at least one numeric component to be selected as the result field.
  • Create a new Calculation - number component.
  • Select the arithmetic operation and the result field.
  • Optionally the number of decimals can be configured.
  • Configure the rest of options like a normal webform component.

For Webform Calculation hidden (7.x-1.x)

Go to add content > webform (or any webform content type):

  • Create a new webform
  • Create new webform components, you need at least two numeric and a select component.
  • Create a new Calculation - hidden component.
  • Select the operand field. It has to be one or more of the numeric fields created previously. Hold Control key to select more than one.
  • Select the arithmetic operation and the result field.
  • Select the select field (created previously).
  • Select the select field choices. The choices must coincide with the chosen select field options. Hold Control to key to select more than one.
  • Select the result field, it has to be one of the numeric fields created previously.
  • If you want to include the current value of the result field in the calculation, set the cumulative result to Yes, (E.g. for quotations) otherwise choose No (E.g. for formulas).
  • Configure the rest of options like a normal webform component.

This tutorial is a quick introduction to the module:

Webform calculation components quick tutorial - Drupal 7

Webform Calculation Components 7.x-2.x

Instead of one operand field in the 7.x-2.x version there are two operand fields. With commutative operations like addition, multiplication etc. you can include all the fields in either operand. With non commutative operations like e.g. division, all the fields in the first operand are multiplied, so all the fields in the second operand, then the output of the first operand is divided by the output of the second operand. With subtraction instead of multiplied, all first operand fields are added so all second operand fields, then both outputs subtracted to calculate the total.
Another change is that the cumulative result option has been removed. To make the result cumulative, include it in the second operand field.

Upgrading from Webform Calculation Components 7.x-1.x to 7.x-2.x

If you have an existing site on Webform Calculation Components 7.x-1.x with webform nodes that use calculation hidden components the following warning message may appear after upgrading to the 7.x-2.x version:

Warning: Invalid argument supplied for foreach() in WebformCalculationComponentsFormTree->getOperationData() (line 141 of /mydrupalsitename/sites/all/modules/webform_calculation_components/WebformCalculationComponentsFormTree.php).
Warning: Invalid argument supplied for foreach() in WebformCalculationComponentsFormTree->getOperationData() (line 144 of /mydrupalsitename/sites/all/modules/webform_calculation_components/WebformCalculationComponentsFormTree.php).

You will also notice that no operand field is selected in the settings. To solve it just edit all the calculation hidden components created previously (with the 7.x-1.x version) and configure it again filling up the new fields like first operand and second operand, then save.

Notes:
- Division by zero is prevented internally.
- Nested webform components in fieldset are supported.

Related modules

This module differs with webform calculator in the following features:

  • The user interface of webform calculator is on the webform body. To create a formula you need to enter a field with value e.g. formkey1 * 0.1 * formkey2. Then the tokens are replaced in the webform body.
  • This module does not use tokens, you add webform components like when creating any other webform fields, using the default webform interface for the configuration.
  • Unlike this module, webform calculator uses the Jeditable library.
  • With this module you can choose the method of triggering the calculation. Adding a calculation number component, the result is calculated after the user introduces a number in the field (this is recommended in the case of a formula). With calculation hidden components the result is calculated when the user checks a select input, a check box or a radio button (this is recommended for spreadsheets or quotations).
  • This module also has the option of including or excluding the amount of the result field in the calculation. That is achieved checking or not the cumulative option in the configuration of the calculation hidden component.

Dependencies
**************
- webform (>=7.x-4.x)

Note: due to incompatibilities between webform 7.x-4.x API and previous versions API, this module requires webform version 4.0 or above.

Project information

Releases