TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 79 of modules/contrib/domain/domain/src/DomainElementManager.php).

Created on 29 August 2024, 5 months ago

Drupal Version

10.3.0

Domain module version

2.0.x-dev

Expected Behavior

The user Add/Edit page is loaded and Domain configurations can be configured.

Actual Behavior

Error on User add/edit page
TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 79 of modules/contrib/domain/domain/src/DomainElementManager.php).

Steps to reproduce

- Install and enable the module
- Enable Domain Access & Domain content
- Configure the Domain
- Enable the Account form Domain fields
- Add/Edit a user
- Error will be shown

🐛 Bug report
Status

Active

Version

2.0

Component

Code

Created by

🇮🇳India joshua1234511 Goa

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

Merge Requests

Comments & Activities

  • Issue created by @joshua1234511
  • Pipeline finished with Success
    5 months ago
    Total: 228s
    #267703
  • Status changed to Needs review 5 months ago
  • 🇮🇳India joshua1234511 Goa

    Submitted a merge request. Ready for review.

  • Status changed to Needs work 5 months ago
  • 🇫🇷France dqd London | N.Y.C | Paris | Hamburg | Berlin

    The solution is somewhat doubling the condition check and could be more performant by using nested checks:

    instead of

    // @@ -76,7 +76,8 @@ class DomainElementManager implements DomainElementManagerInterface {
    
    if ((isset($form[$field_name]['widget']['#options']) &&
        count($form[$field_name]['widget']['#options']) === 0)
        ||
        (isset($form[$field_name]['widget']['#options']) &&
        count($form[$field_name]['widget']['#options']) === 1 &&
        isset($form[$field_name]['widget']['#options']['_none']))
    ) {
      $empty = TRUE;
    

    maybe like:

    // @@ -76,7 +76,8 @@ class DomainElementManager implements DomainElementManagerInterface {
    
    if ((isset($form[$field_name]['widget']['#options']) 
        if (count($form[$field_name]['widget']['#options']) === 0)
    	||
            (count($form[$field_name]['widget']['#options']) === 1 &&
            isset($form[$field_name]['widget']['#options']['_none']))
        )
    ) {
      $empty = TRUE;
    
  • 🇮🇷Iran amir jamshidi

    I made this condition like this

    if ((isset($form[$field_name]['widget']['#options']) &&
             count($form[$field_name]['widget']['#options']) === 0)
            ||
            (isset($form[$field_name]['widget']['#options']) &&
             count($form[$field_name]['widget']['#options']) === 1 &&
             isset($form[$field_name]['widget']['#options']['_none']))
        ) {
          $empty = TRUE;
        } 
  • While I can confirm the code in #5 does work on D10.3, it is simply a repetition of what's in the merge request mentioned in #3. I agree with dqd in #4 that the change could be rewritten, but the code in that comment isn't quite right and would be a breaking change, so I've tweaked it in the patch attached here, which does work for me.

  • 🇺🇸United States earthday47 New York

    Here's my +1 comment - We are experiencing this issue and I can confirm that the patch in #6 resolves it.

  • 🇬🇧United Kingdom therobyouknow

    I also found #6 https://www.drupal.org/project/domain/issues/3470873#comment-15844907 🐛 TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 79 of modules/contrib/domain/domain/src/DomainElementManager.php). Active to work, in that I don't now see that fatal error, the title of this issue "TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in count() (line 79 of modules/contrib/domain/domain/src/DomainElementManager.php)". I needed to clear cache.

    Thank you!

    However, I get a warning on the node add/edit page: "Warning: Undefined array key "#options" in Drupal\domain\DomainElementManager->setFormOptions() (line 79 of modules/contrib/domain/domain/src/DomainElementManager.php)."

    But I can get by. Hope for a fix for the warning (or even to help provide one), when I can.

  • Status changed to Needs review 20 days ago
  • 🇪🇸Spain guiu.rocafort.ferrer Barcelona

    I managed to reproduce this error when using a "Autocomplete" or "Autocomplete (Tags style)" for the field "Domain Access" in the manage form display configuration for the content type.

    Clearly the code does not contemplate the possibility of using those field widgets for this field, so the patches do not fix the root cause of the issue.

Production build 0.71.5 2024