throw an exception when IDs passed to loadMultiple() are badly formed

Created on 30 June 2021, almost 3 years ago
Updated 17 March 2023, over 1 year ago

Problem/Motivation

There are lots of issues about ContentStorageBase producing a warning 'Warning: array_flip(): Can only flip STRING and INTEGER values'.

This happens here:

    $flipped_ids = $ids ? array_flip($ids) : FALSE;

If the $ids array can't be flipped, then that means it contains things that aren't valid entity IDs, such as arrays. So the parameter passed in is incorrect.

It also means the entity load isn't going to work as expected.

The method should throw an exception to allow for better DX.

(Adding related issues which propose different methods for improving this.)

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

✨ Feature request
Status

Needs work

Version

10.1 ✨

Component
EntityΒ  β†’

Last updated about 6 hours ago

Created by

πŸ‡¬πŸ‡§United Kingdom joachim

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.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

  • πŸ‡¬πŸ‡§United Kingdom hugronaphor

    Edge-case:

    I have 0, 1 or 2 entities to load and the order matters in my case.
    For performance reasons I want to load them `loadMultiple()`

    $nodeIds = [
      $queryPrev->execute()->fetchField(),
      $queryNext->execute()->fetchField(),
    ];
    $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nodeIds);
    return [
      '#prev' => !empty($nodes[$nodeIds[0]]) ? $nodes[$nodeIds[0]] : NULL,
      '#next' => !empty($nodes[$nodeIds[1]]) ? $nodes[$nodeIds[1]] : NULL,
    ];
    

    To avoid the warning you need extensive logic. E.g:

    if (count(array_filter($nodeIds)) === 2) {
      $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nodeIds);
    }
    elseif (!empty($nodeIds[0])) {
      $nodes = [
        $this->entityTypeManager->getStorage('node')->load($nodeIds[0]),
        NULL,
      ];
    }
    elseif (!empty($nodeIds[1])) {
      $nodes = [
        NULL,
        $this->entityTypeManager->getStorage('node')->load($nodeIds[1]),
      ];
    }
    

    But if you would have 3, 4, 5 entities to load multiple single loads would be needed.

    I'm not demanding changes, just wanted to point out to this DX

Production build 0.69.0 2024