PHP warnings in array_flip in Drupal\Core\Entity\EntityStorageBase->loadMultiple()

Created on 3 June 2020, over 4 years ago
Updated 10 May 2023, over 1 year ago

The Drupal system I have inherited is throwing a large number of PHP warnings into the logs that look like this:

Warning: array_flip(): Can only flip STRING and INTEGER values in Drupal\Core\Entity\EntityStorageBase->loadMultiple()

I have spent some time running it through a debugger and reading existing tickets, this is what I have found:

There seem to be a number of circumstances that can lead to this error. Some error reports I have seen talk about it being caused by an ID value being turned into a float rather than an int, due to an earlier call to floor().

In my case the error occurs in the loadMultiple() method when an ID arrives wrapped in a Drupal\Core\Render\Markup object rather than being a plain integer or string.

In all cases I've seen, the value is correct, but is just not presented in a format that array_flip can handle.

It therefore seems to me that an appropriate fix would be to add a little pre-processing to normalise the array to contain only strings or integers.

I therefore propose that we add the following code to the loadMultiple() method, before the call to array_flip():

    if ($ids) {
      foreach ($ids as $key=>$value) {
        if (!is_string($value) && !is_integer($value)) {
          $ids[$key] = (string)$value;
        }
      }
    }
πŸ› Bug report
Status

Closed: duplicate

Version

9.5

Component
EntityΒ  β†’

Last updated about 10 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom Spudley

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.

Production build 0.71.5 2024