To automatically add values together in a Webform, you need at least 3 fields: 2 or more containing the numbers you want to add, and one that does the actual math and contains the total. This tutorial will walk you through the steps of creating an auto-sum set of fields.
Consider an example where you want the user to rate 3 categories so that the total equals 100.
Create a "Number" field for each category you want to add. For each one, you probably want to set additional settings, such as the following
Repeat these steps for every category you want to add.
This is the field that does the work of adding the numbers. Because it is going to use math, it has to rely on the Twig engine that works behind the scenes in Drupal. Under the "Computed" section of the Fields creation screen, choose "Computed Twig."
The magic of this field happens in the section, "Computed value/markup." It starts out looking small, but you can put lots of code in it and it will expand to fit.
Then use the keys for the form fields that you created in step 1 (in the format "data.form_field_key"), and follow them with + signs to tell Twig to add them.
The code will look something like this:
{{ data.first_field + data.second_field + data.third_field }} |
Related articles appear here based on the labels you select. Click to edit the macro and add or change labels.
|