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

Created on 11 September 2024, 2 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.

Example code

See attached files.

πŸ› 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

Comments & Activities

  • Issue created by @thomasdegraaff
  • Please add to the issue summary your code implementation that triggers this bug.

  • πŸ‡³πŸ‡±Netherlands thomasdegraaff

    Update: The issue is not resolved by adding

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

    Because then the checkboxes are not show in the webform when you accept the defaults without changing anything. They do appear though when you manually change a default option. Then all default options are shown.

  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY

    We can't debug custom code, but conceptually, I understand the checkboxes default value should be an array and not an empty string.

    The attached patch might fix your issue, you could change #default_value when you extend the WebformCheckboxes plugin.

Production build 0.71.5 2024