Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: OpenID Connect / OAuth client 3.0.0-alpha9 New alpha version released for module openid_connect (3.0.0-alpha9). Release: Opensolr Search 4.5.0 Minor update available for module opensolr_search (4.5.0). Release: Timelinr 1.0.1 Minor update available for module timelinr (1.0.1). Usage Milestone: Simplify Module simplify crossed 10,000 active installs. Usage Milestone: Views Reference Filter Module entityreference_filter crossed 10,000 active installs. Usage Milestone: Dropdown Language Module dropdown_language crossed 10,000 active installs. Usage Milestone: Paragraphs Browser Module paragraphs_browser crossed 10,000 active installs. Usage Milestone: OpenAPI Module openapi crossed 10,000 active installs. Usage Milestone: Decoupled Router Module decoupled_router crossed 10,000 active installs. Module Revived: Entityqueue Buttons 1.1.2 Module entityqueue_buttons updated after 8 months of inactivity (1.1.2).

Calculation Fields

170 sites Security covered Drupal 9–11
View on drupal.org

This module adds new form field types that can evaluate mathematical expressions and display the results. One type acts as a number input that submits its calculated value, while the other displays the result using custom markup without submitting it. These fields can also be integrated into Webforms.

This module extends the Drupal form API adding two more form element types.

  • Calculation element
  • Calculation element markup

Both elements can evaluate a math expression and display the result.

The Calculation element displays as number input, you can use this value as normal input displaying the result on the front, and the result value will be submitted to the backend.

With Calculation element markup you can provide a specific markup to display the result. The result won't be submitted with the form.

Webform integration.
These elements can also be used in Webforms. Just enable the Webform Calculation fields and you can use these elements on your webform.

How does it work in a form array?
The example below demonstrates how to create the calculation number element.
The #evaluation_fields you gonna inform the math expression.
As you can see in the code below, to use another field from the form inside the expression you can use : it to inform the name of the field.

$form['result'] = [
  '#type' => 'form_calculation_element',
  '#title' => $this->t('Result'),
  '#required' => TRUE,
  '#evaluation_fields' => '(:first_value + :second_value + :third_value) * :multiple',
  '#evaluation_round' => 2,
];

The result will be calculated when all those fields on the expression are populated. Then the field will be replaced by the value and finally, the expression will be executed and the input field will be updated with the result.

You can use any kind of math expression like the examples below.

'#title' => $this->t('BMI = weight (kg) / (height (m) * height (m))'),
'#evaluation_fields' => ':weight / (:height * :height)',
'#title' => $this->t('Loan Repayment Calculation Example'),
'#evaluation_fields' => '(:loan_amount * :interest_rate * (1 + :interest_rate) ^ :loan_term) / ((1 + :interest_rate) ^ :loan_term - 1)',

Additional Requirements

On the front-end side we use the library Mathjs to evaluate the expression. You don't need to install this library.
On the back-end side to evaluate the expression we use symfony/expression-language package.

Community Documentation

If you need more information about how to configure the form elements please take a look on the README that has some examples and it's explained in details. Also, enable the calculation fields examples to see the elements working and feel free to take a look on the form CalculationFieldsExample
Also, to check more details about the integration with webforms please take a look at README and install the webform_calculation_fields_examples module to get some webforms already configured as examples.

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
2
Tracked since
May 2025
Latest release
5 months ago
Releases (12 mo)
1
Maintenance
Active

Releases

Version Type Core Release date
1.0.4 Stable 9–11 Mar 4, 2026
1.0.3 Stable 9–11 May 16, 2025