Problem/Motivation
I'm using a custom entity with a content moderation field. All works fine but after saving a register when the content is loaded I got the following warning message :
Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\ContentEntityStorageBase->loadMultipleRevisions() (line 573 of core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php).
The line 573 is part of a verification to load multiple revisions and the array_flip() function is receiving an array like this
^ array:1 [▼
0 => 103
]and after flipping the array it's returning another like this
^ array:1 [▼
103 => 0
]
In this case the array_flip() is working but is returning the warning message.
$flipped_ids = array_intersect_key(array_flip($revision_ids), $revisions);
Steps to reproduce
- Create a custom entity
- add a content moderation field
- and save some registers.
Proposed resolution
Since I don't detect that flip_array () is receiving a malformed array and if I transform the array manually I get the same results.
I suggest maybe returning a more complete feedback message since I have found a lot of posts with issues with similar messages.
Looking for doesn't skip the verification that ensures that the returned array is ordered the same as the original.
I test using a foreach iteration to do the array flip, I get the same result but I'm avoiding the warning message.
If someone can test the following patch and give me some feedback or suggest an alternative solution it would be great.
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | make-revision-ids-core-check-verify-we-have-non-empty-array.patch | 1.01 KB | jnicola |
| #4 | 3249389-array-flip-replaced-by-foreach-iteration.patch | 985 bytes | carlos-perez |
Issue fork drupal-3249389
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
carlos-perez commentedLooking doesn't skip the validation to ensure that the returned array is ordered the same as the original.
I test flipping the array using a foreach iteration and works well. I'm skipping the warning message returned by the
flip_array()function.If someone can test and give me some feedback or suggest an alternative solution it would be great.
Comment #3
carlos-perez commentedComment #4
carlos-perez commentedComment #5
larowlanThis happens when you pass NULL to a load operation
Can you post your entity annotation?
Comment #6
carlos-perez commentedHi! I'm using a custom workflow and the patch works fine to me in this case on 9.2.x.
Yes, the problem happens in a load operation
public function loadMultipleRevisions(array $revision_ids)Is receiving $revision_ids that is something like:
and flip it to
103=>0.just like when I load some other type of entity.
But with a warning message included.
I was looking for a malformed array or null values on it, but I didn't find it.
If there is a way to resolve without a patch would be nice.
This is the entity annotation
Comment #7
larowlancan you var_export the array and share? there may be some casting going on with the new patch
Comment #8
carlos-perez commentedThis is the exported var
array ( 0 => 109, )maybe is expecting an element after the comma.
Comment #9
larowlanthat looks like the second entry is NULL somehow?
Comment #10
larowlanhttps://3v4l.org/4oWIV would suggest so
Comment #11
carlos-perez commentedYes, thank you so much.
I'll try to track where the array is built.
Comment #13
gaurav_manerkar commentedI am facing this issue as well after updating to 9.3
Comment #14
cilefen commentedIt seems we need more information to provide support for this one. Reopen it if there is some.
Comment #15
jnicola commentedI'm having the same issue creating a group entity in Behat from simple array values and a content moderation state.
Comment #16
jnicola commentedAlso, just tested the patch locally. Resolved my issues.
Issue was programmatically creating a pretty simple group from array values. No idea where the array flip complaint came from.
This is likely an issue worth getting some review on. A simple start would be to create a bare bones drupal install with just content moderation and programatically create an entity to see if it can be reproduced, and increase complexity until the warning shows up.
Comment #17
jnicola commentedAlright, another update.
After experimenting with this, I was able to ascertain that $revision_ids is an array of a single NULL value when programmatically creating content with content moderation on it. In our case it is a group content type. We're experiencing this in 9.5.x, creating a simple group entity with very simple values.
I tried using the foreach patch above, which worked... but when standing the site up for automated testing it would error out on config imports due to memory limitation issues. Not sure how the two relate?
I was able to resolve the issue by instead of using a foreach, adding a few sanity checks prior to the array_flip causing the warning:
Attached is the patch, which does not follow the correct issue queue naming conventions, but eh whatever take it out of my bonus that I don't get ;)
Hope this helps somebody, and perhaps with some simpler steps to reproduce and a test we could get this onwards to core.
Comment #19
franceslui commentedThe current implementation of ContentEntityStorageBase::loadRevision() and ContentEntityStorageBase::loadMultipleRevisions() does not adequately handle invalid or empty revision IDs. This can lead to potential errors or unexpected behavior when these methods are called with invalid parameters.
Solution:
1. Added validation in ContentEntityStorageBase::loadRevision($revision_id) to check if $revision_id is either an integer or a string. If not, the method exits early and returns NULL.
2. Added a check in ContentEntityStorageBase::loadMultipleRevisions(array $revision_ids) to exit early and return the revisions array if no revisions can be loaded, ensuring the method does not proceed with empty data.
Comment #20
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue.
While you are making the above changes, we recommend that you convert this patch to a merge request. Merge requests are preferred over patches. Be sure to hide the old patch files as well. (Converting an issue to a merge request without other contributions to the issue will not receive credit.)
Comment #23
prashant.cRaised a PR by taking the changes from #19.
Changing the status to NR.
Thanks
Comment #24
smustgrave commentedSupport requests typically don't have patches or MRs.
Has anyone determined where the issue is happening? Instead of just putting a check that may be masking a larger issue.
Comment #25
franceslui commentedI concur with @smustgrave’s comment #24 that the check I implemented in my patch might obscure a deeper issue. Therefore, I have removed my patch from this issue. Upon further investigation, I discovered that the problem stems from the static_page module rather than Drupal core. The detailed information can be found in the related issue at #3386719: Fatal error when viewing a static_page revision due to core type upcasting change. You can also find my new patch in my comment at #3386719-4: Fatal error when viewing a static_page revision due to core type upcasting change.
Comment #26
quietone commentedComment #27
smustgrave commentedBelieve the related issue should solve this support request.