
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
Comment | File | Size | Author |
---|---|---|---|
#23 | 3221534-nr-bot.txt | 149 bytes | needs-review-queue-bot |
Issue fork drupal-3221534
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
joachim CreditAttribution: joachim at Factorial GmbH commentedSomething like this:
Comment #5
guilhermevp CreditAttribution: guilhermevp at CI&T commentedFollowed @joachim suggestion since seemed good for me. Just changed
$bad_ids
to$invalid_ids
that I felt was more clear. Please review.Comment #6
daffie CreditAttribution: daffie commentedFix looks good. Now it needs a test.
Comment #7
joachim CreditAttribution: joachim as a volunteer commentedDone.
Comment #8
daffie CreditAttribution: daffie commented@joachim: Thank you for adding the testing.
Just a couple of nitpicks and than it is RTBC for me.
Comment #9
joachim CreditAttribution: joachim as a volunteer commentedFixed all three. (Gitlab is not letting me mark the comments as resolved??)
Comment #10
daffie CreditAttribution: daffie commentedThe change look good to me.
The fix has testing for it.
The new exception has documentation.
For me it is RTBC.
@joachim: Only the person who creates the MR is allowed to mark threads as resolved at the moment. I am just working with the tools that we have.
Comment #11
joachim CreditAttribution: joachim as a volunteer commentedI was sure I'd marked someone else's comments as resolved before! Oh well!
Thanks for the review :)
Comment #12
seanbNot really sure about throwing an exception here. Loading entities is something that is done A LOT in custom and contrib code. As mentioned in #3037956: Improve DX of load / loadMultiple / loadRevisionload / loadMultipleRevisions:
While you could argue the calling code should not call the method with "garbage" input, this means that calling code always needs to do 2 checks:
1. Is the input a string or int.
2. Is the return value of the specific load method not NULL.
It would be nice if you could call
load()
/loadMultiple()
/loadRevision()
/loadMultipleRevisions()
without having to check the input, and let the load methods return NULL if it can't find an entity. I've seen methods (likeContentEntityStorageBase::getLatestTranslationAffectedRevisionId()
) that could return a entity ID or NULL and I think the DX would be a bit better if you don't have to do the extra check.Comment #13
joachim CreditAttribution: joachim as a volunteer commented> While you could argue the calling code should not call the method with "garbage" input, this means that calling code always needs to do 2 checks
I've often hit this problem with migrations, where I definitely *do* want an exception to be thrown so I understand why the migration isn't running correctly.
Comment #14
catchThrowing an exception here can only change in a major release - there is plenty of contrib and custom code out there resulting in this error, which will result in fatal errors on production sites if we just change it in a minor.
However we can prepare to throw the exception and log a more useful error message (and maybe a backtrace) in the meantime.
Also pretty sure there are old issues for this around.
Comment #15
joachim CreditAttribution: joachim at Factorial GmbH commented> However we can prepare to throw the exception
Is there anything you mean beyond having a MR ready for Drupal 10?
Is there an equivalent to a deprecation error that would say 'This call will cause an exception to be thrown in a future version of Drupal'?
Comment #16
daffie CreditAttribution: daffie commentedWe can do a @trigger_error(), just like a deprecation message that in D10 the message will be replaced by an exception.
Comment #17
catchYes exactly what @daffie said (and also the second half of my sentence).
Comment #21
joachim CreditAttribution: joachim as a volunteer commentedRebased to 9.5.x (I'm not yet working on 10, need up upgrade my MAMP to get PHP 8.1).
Comment #22
joachim CreditAttribution: joachim as a volunteer commented> We can do a @trigger_error(), just like a deprecation message that in D10 the message will be replaced by an exception
Done.
Comment #23
needs-review-queue-bot CreditAttribution: needs-review-queue-bot as a volunteer commentedThe 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.
Comment #24
hugronaphor CreditAttribution: hugronaphor as a volunteer commentedEdge-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()`
To avoid the warning you need extensive logic. E.g:
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
Comment #26
rakshith.thotada CreditAttribution: rakshith.thotada as a volunteer and at Axelerant commentedRerolled the patch for Drupal 10.2.x
Comment #27
smustgrave CreditAttribution: smustgrave at Mobomo commentedCurrent development branch is 11.x and changes should be in MRs
Comment #28
rakshith.thotada CreditAttribution: rakshith.thotada as a volunteer and at Axelerant commentedRemoving the patch as we already have the patch here https://www.drupal.org/files/issues/2023-01-12/3145190-21.patch