Webforms: "falsy" default/settings values not migrated. Fix incorrect usage of empty()

Created on 27 October 2022, about 2 years ago
Updated 26 July 2023, over 1 year ago

Problem/Motivation

If I have a select field in D7 containing the numbers 0-10 as options, and configure the default value to be "0", that default will not be migrated, as "0" is a falsy value. empty("0") returns TRUE.

There are probably other settings where this mistake is made.

Steps to reproduce

      if (!empty(trim($element['value'])) && (empty($valid_options) || in_array($element['value'], $valid_options))) {
        $new_element['#default_value'] = trim($element['value']);
      }
      if (!empty($extra['field_prefix'])) {
        $new_element['#field_prefix'] = $extra['field_prefix'];
      }
      if (!empty($extra['field_suffix'])) {
        $new_element['#field_suffix'] = $extra['field_suffix'];
      }
...
        if (!empty($extra['description'])) {
          $new_element['#description'] = $extra['description'];
        }

All of these should allow "0" as valid values. It's possible to set "0" or "0.0" as prefix and suffix as well.

Proposed resolution

      if (trim($element['value']) !== '' && (empty($valid_options) || in_array($element['value'], $valid_options))) {
        $new_element['#default_value'] = trim($element['value']);
      }
      if (isset($extra['field_prefix'])) {
        $new_element['#field_prefix'] = $extra['field_prefix'];
      }
      if (isset($extra['field_suffix'])) {
        $new_element['#field_suffix'] = $extra['field_suffix'];
      }
...
        if (isset($extra['description'])) {
          $new_element['#description'] = $extra['description'];
        }

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

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