Computed Twig PHP 8.x

Created on 14 October 2022, about 2 years ago
Updated 11 February 2023, almost 2 years ago

Problem/Motivation

I have been using the computed twig element in my webforms for some time and has worked perfectly. Since updating my sites to Drupal 9.4.x and updating the server php version to either 8.0.x or 8.1.x, the calculations no longer work. I have to switch back to php7.4.x for this to start working again.

Steps to reproduce

Switch from php 7.4.x to php 8.x
This is an example of one of my computed value/markups:
{{ (data.sockets * data.socketcost)+ (data.switches * data.switch_cost) + (data.outdoorlight * data.outdoor_light_cost) }}

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Closed: works as designed

Version

6.2

Component

Code

Created by

🇬🇧United Kingdom mylocaltrades

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.

  • 🇬🇧United Kingdom mylocaltrades

    So I have finally got around to working on this issue again and now have the solution in case it helps anyone else.

    The computed twig used before php8:

    sockets:
    '#type': textfield
    '#title': 'How many sockets would you like?'
    socketcost:
    '#type': hidden
    '#title': 'socket cost'
    '#default_value': '65.00'
    '#prepopulate': true
    total_cost:
    '#type': computed_twig
    '#title': 'Total cost'
    '#title_display': invisible
    '#template': '

    £{{ (data.sockets * data.socketcost) }}
    excluding VAT

    '
    '#ajax': true

    This above would multiply the amount of sockets by each individual socket cost. I can confirm this does not work in PHP8>

    Below is how it now works in PHP8> - I use the number field instead of the textfield and the value field for the individual cost price:

    sockets:
    '#type': number
    '#title': 'How many sockets would you like?'
    socketcost:
    '#type': value
    '#title': socketcost
    '#value': '65'
    balance:
    '#type': computed_twig
    '#title': 'Total cost'
    '#mode': text
    '#template': '{{ (data.sockets|number_format * data.socketcost) }}
    '#whitespace': trim
    '#ajax': true

Production build 0.71.5 2024