Search code examples
twig

sum values of selected radios in twig


I want to sum up all values of selected radios (all q_ keys checked values) in a Drupal Webform form. Now I am doing it like this data.q_1 + data.q_2 + data.q_3 + data.q_4 + data.q_5 but I want a more efficient way to avoid having to type all the data.q_# as there are many q_ in each group. I tried this (in a computed_twig element in Webform) with the reduce filter:

{% set questions = qp1.ps1|filter((v,k) => k starts with 'q_') %}
{{ questions | reduce((carry, v) => carry + v) }}

The above is giving me an Unsupported operand types error.

Here is a minimal sample of my yaml code:

qp1:
  '#type': wizard_page
  '#title': 'Part I'
  '#open': true
  ps1:
    '#type': details
    '#title': title...
    '#required': true
    '#attributes':
      class:
        - qp1_1
    q_1:
      '#type': radios
      '#title': q1.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q_2:
      '#type': radios
      '#title': q2....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q_3:
      '#type': radios
      '#title': q3.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q_4:
      '#type': radios
      '#title': q4.......
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q_5:
      '#type': radios
      '#title': q5........
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true

Solution

  • I finally found a great clean solution and I hope it can save someone the frustration and hours of search and trials and reading to no avail like I did. I simply used this in my computed_twig template. Using a for loop to iterate through the data array and sum up the values of only the keys that start with q_:

        {% set sum = 0 %}
         {% for key, value in data %}
          {% if key matches '/^q_\\d+$/' %}
           {% set sum = sum + value %}
          {% endif %}
         {% endfor %} {{ sum }}
    

    This is working perfectly as expected for my case and saves me having to type too many data.q# as per the number of the tens of questions I have.

    Notes: For the naming I use (q_1, q_2 ...), for each key-value pair in the array, we check if the key matches the pattern /^q_\d+$/. This pattern matches any key that starts with q_ followed by one or more digits. If the key matches this pattern, we add the value to the sum variable. Once the loop is finished, the sum variable will contain the total sum of all the values for keys that match the pattern. You might want to edit the pattern (the q_ part) to match your keys naming.