Dealing with Twig Computed values

Created on 31 December 2021, almost 3 years ago
Updated 4 June 2024, 7 months ago

Problem/Motivation

When using this module with webforms that use a computed field, an error is thrown because the module expects a numeric value but computed fields are returned as objects. I noted this issue before here: https://www.drupal.org/project/commerce_webform_order/issues/3103974 β†’

Steps to reproduce

Create a webform. Add a "Computed Twig" element. Cause that element to have a number. Use that number as the price in the enabled webform handler (or variation, or store, or quantity). Submit. An error will be thrown such as 'The provided value " 45.00 " is not a numeric value.' Do not be fooled, it's not just the extra space, but an entire object that makes the module choke.

Proposed resolution

1. Add a small function as follows

  /**
  *convert an object to a string to avoid error
  *
  **/

  protected function objectSafe($obj){

    if(is_object($obj)){
      $val = trim($obj->__toString());
    }else{
      $val = $obj;
    }

    return trim(str_replace('$', '', $val) );
  
  }

2. Where numeric input is taken from webform submissions, apply the function, eg:
$data['order_item']['amount'] = $this->objectSafe($data['order_item']['amount']);
3. The above line would go before line 997. For ['price'] before line 1015. For ['quantity'], before 1017.

Remaining tasks

Try it out and look for any other number inputs from webform submission that I have overlooked

User interface changes

None

API changes

None

Data model changes

None

✨ Feature request
Status

Fixed

Version

3.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States Cayenne

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024