processCheckboxes: count(): Argument #1 ($value) must be of type Countable|array, null given

Created on 11 September 2024, 6 months ago

Problem/Motivation

I needed to set default options for a webform checkboxes component. I do this by extending htdocs/core/lib/Drupal/Core/Render/Element/Checkboxes.php and htdocs/modules/contrib/webform/src/Plugin/WebformElement/Checkboxes.php and setting the default values in defineDefaultProperties(). The following error then occurs on saving the element: count(): Argument #1 ($value) must be of type Countable|array, null given.

Steps to reproduce

Extend htdocs/core/lib/Drupal/Core/Render/Element/Checkboxes.php and htdocs/modules/contrib/webform/src/Plugin/WebformElement/Checkboxes.php and set the default values for options in defineDefaultProperties(). Then use the element in a webform, and save the element. The following error then occurs: count(): Argument #1 ($value) must be of type Countable|array, null given.

Proposed resolution

Check if $element['#default_value'] is set, if not set it to an empty array:

if (!isset($element['#default_value']) || $element['#default_value'] == 0) {
$element['#default_value'] = [];
}

Remaining tasks

Check if this is a correct solution, and create a pull request.

πŸ› Bug report
Status

Active

Version

6.2

Component

Code

Created by

πŸ‡³πŸ‡±Netherlands thomasdegraaff

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024