Problem/Motivation

After upgrading to PHP 8.0.21 Webform blows up with the error message

TypeError: abs(): Argument #1 ($num) must be of type int|float, string given in abs() (line 766 of /local/drupal/sites/all/modules/webform/components/number.inc

Worked with earlier versions of PHP.

Steps to reproduce

  1. Install webform 7.x-4.24 on a Drupal 7 site
  2. upgrade PHP to 8.0.21
  3. create a form with a number field
  4. try to bring up the form
  5. note the error message

Proposed resolution

--- number.inc  2021-04-07 13:07:13.000000000 -0400
+++ /tmp/number.inc     2022-08-01 09:34:28.961992579 -0400
@@ -763,7 +763,7 @@
  */
 function _webform_number_select_options($component) {
   $options = array();
-  $step = abs($component['extra']['step']);
+  $step = abs($component['extra']['step'] ?: 1);

   // Step is optional and defaults to 1.
   $step = empty($step) ? 1 : $step;

Remaining tasks

Apply patch.

User interface changes

None.

API changes

N/A

Data model changes

None

Issue fork webform-3301125

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bkline@rksystems.com created an issue. See original summary.

bkline’s picture

Issue summary: View changes

immaculatexavier made their first commit to this issue’s fork.

immaculatexavier’s picture

Status: Active » Needs review

Committed in accordance to the proposed resolution. please review

bkline’s picture

Status: Needs review » Reviewed & tested by the community

Works in my environment.

ccarnnia’s picture

Thank you @immaculatexavier .
This worked for webform 7.x-4.24 on 7.91 core with php 8.0.17.
Exported it as a patch while we wait for the MR.

liam morland’s picture

Version: 7.x-4.24 » 7.x-4.x-dev

Thanks. Please make a merge request.

It would be great for a test to be added which would surface this.

solideogloria’s picture

This isn't the best solution. Look at the next line:

  $step = abs($component['extra']['step']);

  // Step is optional and defaults to 1.
  $step = empty($step) ? 1 : $step;

Perhaps the line $step = empty($step) ? 1 : $step; should come first, instead of adding ?: 1

This issue happens when step isn't set in the options, which makes the $component['extra']['step'] empty string. Switching the line order should fix it.

Alternatively, remove the second line and use $step = abs($component['extra']['step'] ?: 1);

solideogloria’s picture

Status: Reviewed & tested by the community » Needs review
solideogloria’s picture

I confirmed it fixes the issue for me. This is just an improved version, so should be a quick RTBC.

bkline’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

solideogloria’s picture

Priority: Normal » Major

I'm marking this issue major, as it prevented users from submitting certain webforms after I switched to PHP 8.

liam morland’s picture

Status: Reviewed & tested by the community » Fixed

Thanks everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.