Value of computed twig is not being stored correctly

Created on 23 July 2023, over 1 year ago

Problem/Motivation

I am trying to use the webform entity handler to store data from computed twig into a content form. The computed twig formulat calculated a number and stores it in the database. However the content created by the handler only stores the value as 1 in the field.

Steps to reproduce

Create a webform handler to add values to a node. The field type for the node is set to float. The computed field calculates a number ranging between 0 - 100.

The formula for the computed twig is as follows :

{% set raw_score = (data.c30_q1+data.c30_q2+data.c30_q3+data.c30_q4+data.c30_q5)/5 %}
      {% set score = (1 -((raw_score-1)/3))*100  %}
      {{ (score|round(1,'ceil')) }}

here c30_q1, c30_q2.. are fields that output numbers between 1 - 4.

The handler stores is supposed to store the field value obtained from the computed twig into a float field. It returns 1 always. However when I use a text field the numerical value is stored correctly.

Note that I encountered an error when I tried to save the data to a decimal field but not to a float or integer field. In the former case, the following error came on the submission of the webform (note that the webform got submitted but the content did not get created.

TypeError: round(): Argument #1 ($num) must be of type int|float, Drupal\Core\Render\Markup given in round() (line 126 of /opt/drupal/web/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/DecimalItem.php).

I have tried to change the last line of the code to return the value without rounding up and it still throws this error.

Finally and very interestingly the c30_q1, c30_q2 field data is also being stored in the same content in integer fields and they seem to store the data fine.

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇮🇳India santam

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024