user_load should return false when empty array is passed as an argument

Created on 15 April 2023, over 1 year ago

Problem/Motivation

I've been getting the "Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet()" warning.

I have a view which pulls a user picture from relationship. Sometimes the relationship returns empty data (which is normal in my case), but user data is still being tried to be pulled by the views module. I traced it down to this function being called: user_load(array("")).

Steps to reproduce

Call user_load with an array with an empty value as an argument (i.e user_load(array("")) ).

This is the debug_backtrace:

    [0] => Array
        (
            [function] => {closure}
            [class] => DrupalDefaultEntityController
            [type] => ->
            [args] => Array
                (
                    [0] => 2
                    [1] => array_flip(): Can only flip STRING and INTEGER values!
                    [2] => /home/campmystic/public_html/includes/entity.inc
                    [3] => 405
                    [4] => Array
                        (
                            [ids] => Array
                                (
                                    [0] => 
                                )

                            [conditions] => Array
                                (
                                )

                            [entities] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [file] => /home/campmystic/public_html/includes/entity.inc
            [line] => 405
            [function] => array_flip
            [args] => Array
                (
                    [0] => Array
                        (
                            [0] => 
                        )

                )

        )

    [2] => Array
        (
            [file] => /home/campmystic/public_html/includes/entity.inc
            [line] => 179
            [function] => cacheGet
            [class] => DrupalDefaultEntityController
            [type] => ->
            [args] => Array
                (
                    [0] => Array
                        (
                            [0] => 
                        )

                    [1] => Array
                        (
                        )

                )

        )

    [3] => Array
        (
            [file] => /home/campmystic/public_html/includes/common.inc
            [line] => 8173
            [function] => load
            [class] => DrupalDefaultEntityController
            [type] => ->
            [args] => Array
                (
                    [0] => Array
                        (
                            [0] => 
                        )

                    [1] => Array
                        (
                        )

                )

        )

    [4] => Array
        (
            [file] => /home/campmystic/public_html/modules/user/user.module
            [line] => 291
            [function] => entity_load
            [args] => Array
                (
                    [0] => user
                    [1] => Array
                        (
                            [0] => 
                        )

                    [2] => Array
                        (
                        )

                    [3] => 
                )

        )

    [5] => Array
        (
            [file] => /home/campmystic/public_html/modules/user/user.module
            [line] => 366
            [function] => user_load_multiple
            [args] => Array
                (
                    [0] => Array
                        (
                            [0] => 
                        )

                    [1] => Array
                        (
                        )

                    [2] => 
                )

        )

    [6] => Array
        (
            [file] => /home/campmystic/public_html/sites/all/modules/views/modules/user/views_handler_field_user_picture.inc
            [line] => 109
            [function] => user_load
            [args] => Array
                (
                    [0] => 
                )

        )

Proposed resolution

Check for empty arrays and return false without triggering a warning.

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Closed: works as designed

Version

7.0 ⚰️

Component
User moduleΒ  β†’

Last updated 1 day ago

Created by

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

Comments & Activities

  • Issue created by @ethan1el
  • Status changed to Closed: works as designed over 1 year ago
  • πŸ‡ΈπŸ‡°Slovakia poker10

    Thanks for reporting this. If you take a look at the documentation of the user_load() function:

    Parameters
    $uid: Integer specifying the user ID to load.

    $reset: TRUE to reset the internal cache and load from the database; FALSE (default) to load from the internal cache, if set.

    Then you can see that you have to pass an integer as the first parameter. Anything else is just a wrong input and Drupal core should not "babysit" a broken code (either custom, or potentially some bugs from views module).

    Also if you take a look at the documentation of the user_load_multiple() function:

    Parameters
    $uids: An array of user IDs.

    $conditions: (deprecated) An associative array of conditions on the {users} table, where the keys are the database fields and the values are the values those fields must have. Instead, it is preferable to use EntityFieldQuery to retrieve a list of entity IDs loadable by this function.

    $reset: A boolean indicating that the internal cache should be reset. Use this if loading a user object which has been altered during the page request.

    Then you can see that the first parameter is an array, but it has to be an array of integers (empty string is not allowed here). Therefore the same applies as above.

    I am changing the status to Works as designed, as the problem seems to be outside of the Drupal core. If there is a bug in the Views module, please fill up the issue in the views issue queue β†’ .

Production build 0.71.5 2024