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

Created on 23 December 2022, over 2 years ago
Updated 31 January 2023, over 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 11 days ago

Created by

๐Ÿ‡ฎ๐Ÿ‡ณIndia keshav patel

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

Merge Requests

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.

  • First commit to issue fork.
  • Merge request !12855Issue #3328962 by ckng: fixed TypeError. โ†’ (Open) created by ckng
  • ๐Ÿ‡ฒ๐Ÿ‡พMalaysia ckng

    Take a different approach to check if roles is empty. MR12855 and patch file attached.
    Updated steps to reproduce.

  • ๐Ÿ‡ฒ๐Ÿ‡พMalaysia ckng
  • Pipeline finished with Success
    about 2 months ago
    Total: 700s
    #558694
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Think we need steps that donโ€™t involve a contrib module. Also may have to do some more backtracking as putting just an empty() check could be masking a larger issue

Production build 0.71.5 2024