TypeError: key(): Argument #1 ($array) must be of type array, string given in key()

Created on 23 December 2022, almost 2 years ago
Updated 31 January 2023, almost 2 years ago

Problem/Motivation

TypeError: key(): Argument #1 ($array) must be of type array, string given in key() (line 362 of core/modules/user/src/AccountForm.php).
Using PHP Version 8.0.26.

Steps to reproduce

The code is:
$form_state->getValue('roles') which was supposed to be of type array, is of type string.

if (is_string(key( $form_state->getValue('roles')))) {
      $form_state->setValue('roles', array_keys(array_filter($form_state->getValue('roles'))));
    }

Proposed resolution

To resolve this, typecast it into an array.

if (is_string(key( (array) $form_state->getValue('roles')))) {
      $form_state->setValue('roles', array_keys(array_filter($form_state->getValue('roles'))));
    }

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Needs work

Version

9.4

Component
User module 

Last updated about 5 hours ago

Created by

🇮🇳India keshav patel

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States smustgrave

    Per #6

  • 🇧🇩Bangladesh shafiqhossain

    I faced the similar issue, posted here, https://www.drupal.org/project/drupal/issues/3477465 🐛 TypeError: key(): Argument #1 ($array) must be of type array, null given in key() (line 368 of /public_html/web/core/modules/user/src/AccountForm.php) Active
    I checked with new Drupal 10 dump with a image field added into user entity, the issue does not persist there.

    But For my existing site, I literally uninstalled all custom modules and most of the contributed modules, but still getting issue. So dump the $form_state->getValues() there and see if "roles" values are there, but its not. I got this:

    Array
    (
        ....
        [3] => field_test_picture
        ....
        [20] => field_user_other_website
        [21] => field_user_picture
        [23] => field_user_state
        [24] => field_user_twitter
        [25] => field_user_youtube
        .....
        [26] => path
        [27] => user_picture
        [28] => submit
        [29] => form_build_id
        [30] => form_token
        [31] => form_id
        [32] => contact
        [33] => field_test_picture_0_upload_button
        [34] => mail
        [35] => pass
        [36] => field_parent_account
    
    
  • 🇧🇩Bangladesh shafiqhossain

    As it mentioned in the AccountForm.php, this process is called twice:

      /**
       * {@inheritdoc}
       */
      public function buildEntity(array $form, FormStateInterface $form_state) {
        // Change the roles array to a list of enabled roles.
        // @todo Alter the form state as the form values are directly extracted and
        //   set on the field, which throws an exception as the list requires
        //   numeric keys. Allow to override this per field. As this function is
        //   called twice, we have to prevent it from getting the array keys twice.
    

    I debugged, in the first call "roles" key is there. But in the second call, "roles" is absent.

Production build 0.71.5 2024