Warning: key() expects parameter 1 to be array, string given in /var/www/html/docroot/core/modules/user/src/AccountForm.php on line 312
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | 3001271-warning-during-form-saving-14.patch | 1.39 KB | ey |
| #12 | 3001271-warning-during-form-saving-12.patch | 1.2 KB | ey |
| #9 | 3001271-9.patch | 1.27 KB | mdolnik |
| #7 | single_user_role_30032019.patch | 1.22 KB | golchi |
| #5 | 3001271-5.patch | 1.25 KB | mdolnik |
Comments
Comment #2
mdolnik commentedThe 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:
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.
Comment #3
tame4tex commentedComment #5
mdolnik commentedFixed issue in the patch with an error that was occurring when no roles were selected.
Comment #6
mdolnik commentedComment #7
golchi commentedIn 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]));Comment #8
tame4tex commented@golchi what warning are you getting?
Either format for the roles value looks valid. Using the original patch the submitted value will look like
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
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!
Comment #9
mdolnik commentedAdded update to my previous patch to change:
to:
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.Comment #10
kdomenick commentedThe patch in #9 worked for me.
Comment #11
skaughtam using patch #9. this does address issue
Comment #12
ey commentedRe-rolled the patch to the latest dev version with minor code optimizations. Please review.
Comment #13
ey commentedWhy are the tests running on Drupal 7?
Comment #14
ey commentedAttached 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.
Comment #15
joe huggans#14 has fixed the issue for me and I have had no problems thus far. Will update if something crops up later on.
Comment #17
gbisht commentedJust 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
Comment #18
gbisht commentedComment #20
ramonma1989#14 has fixed the issue for me, tested in d8.9.11