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

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

  • 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.

  • Status changed to Needs review 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States jrockowitz Brooklyn, NY
  • Pipeline finished with Failed
    6 months ago
    Total: 623s
    #347993
  • Pipeline finished with Failed
    4 months ago
    Total: 889s
    #403954
  • Pipeline finished with Success
    3 months ago
    Total: 188s
    #423957
  • Pipeline finished with Failed
    6 days ago
    Total: 402s
    #499634
  • Pipeline finished with Failed
    6 days ago
    Total: 795s
    #499636
  • Pipeline finished with Failed
    6 days ago
    Total: 225s
    #499660
  • Pipeline finished with Canceled
    6 days ago
    Total: 67s
    #499664
  • Pipeline finished with Failed
    6 days ago
    Total: 792s
    #499665
  • Pipeline finished with Canceled
    6 days ago
    Total: 307s
    #499669
  • Pipeline finished with Failed
    6 days ago
    Total: 777s
    #499670
  • Pipeline finished with Failed
    6 days ago
    Total: 918s
    #499713
  • Pipeline finished with Failed
    6 days ago
    Total: 768s
    #499748
  • Pipeline finished with Success
    4 days ago
    Total: 693s
    #501202
  • Pipeline finished with Failed
    1 day ago
    Total: 738s
    #503094
  • Pipeline finished with Success
    about 15 hours ago
    Total: 757s
    #504152
  • Pipeline finished with Success
    about 15 hours ago
    Total: 946s
    #504175
  • Pipeline finished with Success
    about 14 hours ago
    Total: 646s
    #504188
Production build 0.71.5 2024