Warning: key() expects parameter 1 to be array, string given in /var/www/html/docroot/core/modules/user/src/AccountForm.php on line 312

Comments

divined created an issue. See original summary.

mdolnik’s picture

StatusFileSize
new1.13 KB

The reason this is happening is because this module is changing the roles value type to something that core does not expect.

In AccountForm::buildEntity() it is expecting the roles value to be an array of selected roles keyed by the role machine name: eg:

[
  'selected_role' =>  'selected_role',
  'un_selected_role' =>  0,
]

Where-as this module is changing the value to a string. eg:
'selected_role'

Changing this module's form alter to add a validation callback which transforms the role value to the expected type will fix this issue.

tame4tex’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: Warning during form saving.patch, failed testing. View results

mdolnik’s picture

StatusFileSize
new1.25 KB

Fixed issue in the patch with an error that was occurring when no roles were selected.

mdolnik’s picture

Status: Needs work » Needs review
golchi’s picture

StatusFileSize
new1.22 KB

In case this might be helpful for someone, I have changed a line in the suggested patch to make it work for me because I was still getting a warning (version 8.x-1.0-alpha1):

$form_state->setValue('roles', [$role]);
instead of
+ $form_state->setValue('roles', array_combine([$role], [$role]));

tame4tex’s picture

@golchi what warning are you getting?

Either format for the roles value looks valid. Using the original patch the submitted value will look like

[
'role_name' = 'role_name',
]

This appears to be the standard format the roles value is returned as if this module isn't installed. \Drupal\user\AccountForm::buildEntity() will then convert it to

[
'0' = 'role_name',
]

Your patch puts it in the latter format right away. So from first glance either format is valid, therefore I am interested in what warning you are getting.

Thanks!

mdolnik’s picture

StatusFileSize
new1.27 KB

Added update to my previous patch to change:

if (empty($role)) {
  return;
}

to:

if (empty($role) || is_array($role)) {
  return;
}

It turns out when an anonymous user creates a new account and an administrator enables the account, when the admin edit's the anon account and forgets to set a role and saves, the role will be set to the incorrect role string 'Array' and pass validation even when required and empty.

This is because the array in the situation above is passed in as ['authenticated'] which ends up getting wrapped again as an array and subsequently converted to a string which results as 'Array'

Also this patch keeps in the array_combine() code as I could not reproduce any errors that @golchi was experiencing.

kdomenick’s picture

The patch in #9 worked for me.

skaught’s picture

Status: Needs review » Reviewed & tested by the community

am using patch #9. this does address issue

ey’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new1.2 KB

Re-rolled the patch to the latest dev version with minor code optimizations. Please review.

ey’s picture

Why are the tests running on Drupal 7?

ey’s picture

Attached patch fixes also the default value of the form in the validation callback. I found that some contributed modules also checking the default value and they expect to a have an array instead of string. Please review.

joe huggans’s picture

#14 has fixed the issue for me and I have had no problems thus far. Will update if something crops up later on.

  • gulab.bisht committed ee1241e on 8.x-1.x
    Issue #3001271 by mdolnik, Елин Й., golchi: Warning during form saving.
    
gbisht’s picture

Just pushed the patch from #14. This seems to fix the warning issue, thanks to everyone for working on it and testing.

Note: Commit also include and small formatting changes

gbisht’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

ramonma1989’s picture

#14 has fixed the issue for me, tested in d8.9.11