1

I'm using this in a computed_twig element and it's fully working when I test it in this fiddle but only works correctly for value "0" while gives "high" for everything else (e.g. 0.1, 0.4, 0.5, etc) when in my webform!
q# are values of radio input type elements. I tried several variations all to no avail! Thanks

Edit: updated my question and added a minimal sample of my webform. I didn't post this at the twig stack because it's working on fiddle and only when in my Drupal Webform it's malfunctioning. I tried with and without the number_format filter kindly suggested by @cilefen but still no change in the result. I'd exhausted my search online and read and saw examples on official Twig and other pages and still couldn't figure out what I'm doing wrong! I don't know much about twig apart from what I learn on the go to meet my special need at the time.
Note: I changed "TsC" in my previous post version to TCount to be more meaningful. Also, it's on a shared host with PHP 7.4 still.
Thanks to everyone who commented so far and I wish that whoever downvoted me straight away after posting explained why so I could learn for future posts.

qp1:
  '#type': wizard_page
  '#title': 'Part I'
  '#open': true
  ps1:
    '#type': details
    '#title': title...
    '#required': true
    '#attributes':
      class:
        - qp1_1
    q1:
      '#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
    q2:
      '#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
    q3:
      '#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
    q4:
      '#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
    q5:
      '#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
    q6:
      '#type': radios
      '#title': q6.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q7:
      '#type': radios
      '#title': q7....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q8:
      '#type': radios
      '#title': q8.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q9:
      '#type': radios
      '#title': q9.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q10:
      '#type': radios
      '#title': q10.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q11:
      '#type': radios
      '#title': q11....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q12:
      '#type': radios
      '#title': q12.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q13:
      '#type': radios
      '#title': q13.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q14:
      '#type': radios
      '#title': q14.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
      '#required': true
    q15:
      '#type': radios
      '#title': q15......
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    q16:
      '#type': radios
      '#title': q16.....
      '#options':
        - 'option 1 with value 0'
        - 'option 2 with value 1'
        - 'option 3 with value 2'
        - 'option 4 with value 3'
      '#required': true
    qs_count:
      '#type': computed_twig
      '#title': 'Total Count'
      '#wrapper_attributes':
        class:
          - score_qp_1
      '#template': 'Total Count: {% set TCount = (data.q1 + data.q2 +  data.q3 +  data.q4 +  data.q5 +  data.q6 +  data.q7 +  data.q8 +  data.q9 +  data.q10 +  data.q11 +  data.q12 +  data.q13 +  data.q14 +  data.q15 +  data.q16)|number_format(0) %} {{ TCount }} <br /> Ratio: {{ TCount / 16 }} <br /> {% if TCount == 0 %} Great {% elseif TCount > 0 and TCount < 0.5 %} Low {% elseif TCount >= 0.5 and TCount < 0.7 %} Moderate {% elseif TCount >= 0.7 %} High {% endif %}'
      '#ajax': true
7
  • I'd love to know why my question was down-voted within almost a minute (? less) of posting and no explanation whatsoever ! Is this an automated badly programmed AI job :-? It couldn't be more frustrating and off-putting to new comers and I noticed it's the worst on this Drupal Stack !
    – Adam888
    Commented Apr 14, 2023 at 7:16
  • 3
    I haven't voted, but could you perhaps highlight how your question relates to Drupal, and isn't just about Twig? It seems like TsC isn't what you expect it to be, which you could confirm by simply printing it?
    – Clive
    Commented Apr 14, 2023 at 9:59
  • 2
    PHP 8 is stricter about types, and that can affect these calculations in Twig. Many bug issues were opened for Webform about this but all were actually programming support requests. We don’t actually know what the objects in your code are and so currently this is unanswerable. And I agree this looks like a Twig programming question.
    – mona lisa
    Commented Apr 14, 2023 at 11:36
  • I think they may be strings, right?
    – mona lisa
    Commented Apr 14, 2023 at 11:38
  • Some have had success with the |number_format filter on string operands which are to be used in computations.
    – mona lisa
    Commented Apr 14, 2023 at 12:34

1 Answer 1

0

I am going to answer myself and it seems the culprit was I was setting the conditionals based on TCount while it should be on the ratio (TCount / 16) instead. My bad! Sometimes the culprit would be too silly to miss (and ? notice) and right in front of us but we fail to see despite the many revisions and even different people. Thanks so much everyone. My template now is:

'Total Count: {% set TCount = (data.q1 + data.q2 +  data.q3 +  data.q4 +  data.q5 +  data.q6 +  data.q7 +  data.q8 +  data.q9 +  data.q10 +  data.q11 +  data.q12 +  data.q13 +  data.q14 +  data.q15 +  data.q16)|number_format %} {{ TCount }} <br /> {% set tRatio = TCount / 16 %} Ratio: {{ tRatio }} <br /> {% if tRatio >= 0.7 %} high {% elseif tRatio > 0 and tRatio <= 0.5 %} low {% elseif tRatio > 0.5 %} moderate {% else %} great {% endif %}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.