Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\ContentEntityStorageBase->loadMultipleRevisions()

Created on 15 November 2021, over 2 years ago
Updated 21 June 2024, 7 days ago

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.

๐Ÿ’ฌ Support request
Status

Postponed: needs info

Version

11.0 ๐Ÿ”ฅ

Component
Entityย  โ†’

Last updated 1 day ago

  • Maintained by
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom @catch
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland @Berdir
  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany @hchonov
Created by

๐Ÿ‡ฒ๐Ÿ‡ฝMexico carlos-perez

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States jnicola

    I'm having the same issue creating a group entity in Behat from simple array values and a content moderation state.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States jnicola

    Also, 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.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States jnicola

    Alright, 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:

    if ($revision_ids && !is_null(reset($revision_ids))) {
    

    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.

  • Status changed to Needs review 29 days ago
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada franceslui

    The 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.

  • Status changed to Needs work 29 days ago
  • The 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.)

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Prashant.c Dharamshala

    Prashant.c โ†’ made their first commit to this issueโ€™s fork.

  • Status changed to Needs review 29 days ago
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Prashant.c Dharamshala

    Raised a PR by taking the changes from #19 ๐Ÿ’ฌ Warning: array_flip(): Can only flip STRING and INTEGER values! in Drupal\Core\Entity\ContentEntityStorageBase->loadMultipleRevisions() Needs work .

    Changing the status to NR.

    Thanks

  • Pipeline finished with Canceled
    29 days ago
    Total: 330s
    #185997
  • Pipeline finished with Success
    29 days ago
    Total: 465s
    #186009
  • Status changed to Needs work 29 days ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Support 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.

  • Status changed to Postponed: needs info 7 days ago
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada franceslui

    I 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 https://www.drupal.org/project/static_page/issues/3386719 ๐Ÿ› Revision object is returned instead of revision id due to core type upcasting change. Needs review . You can also find my new patch in my comment at https://www.drupal.org/project/static_page/issues/3386719#comment-15649482 ๐Ÿ› Revision object is returned instead of revision id due to core type upcasting change. Needs review .

Production build 0.69.0 2024