Computed Twig Example

The Webform A collection of features or functionality that can be enabled on sites. WDS has made several optional modules available to site administrators to enable on their site. For example, the "News" module enables the News content type and provides a News List Block for displaying news on a page. in Drupal has options for creating complex forms that use computed elements, such as the Webform with Computed Twig Field example below.

Webform with Computed Twig Field

Number field with unlimited values and key "unicorns"
Unicorns
more items
Number field with unlimited values and key "centaurs" (allow users to sort is disabled)
Centaurs
more items
Computed Twig field with Ajax updating the computed value
Number of Unicorns: 0
Number of Centaurs: 0
Total Magical Creatures: 0

Twig Code for Previous Field

{% set totalUnicorns = 0 %}
{% for value in data.unicorns  %}
  {% set totalUnicorns = totalUnicorns + value %}
{% endfor %}
{% set totalCentaurs = 0 %}
{% for value in data.centaurs  %}
  {% set totalCentaurs = totalCentaurs + value %}
{% endfor %}
Number of Unicorns: {{ totalUnicorns }}<br/>
Number of Centaurs: {{ totalCentaurs }}<br/>
Total Magical Creatures: {{ ( totalUnicorns + totalCentaurs ) }}

Computed Elements Tutorial Video