Test scenario 1:

  1. create a node with an entityreference to a file_entity (image)
  2. create a view of nodes with entityreference to file_entity
  3. Delete the file_entity by fid referenced in the above node
  4. view the view, you will get an array_flip error.

Solution:

apply the proposed patch, it will provide a useful message telling you what node id that is missing an item. So rather than a useless error message from core , views puts a helpful watchdog message with helpful information.

Another test:

create new view of nodes

Relationships
Comment: Last Comment
Content: Last comment author

Fields
Content: Title
(Last Comment) Comment: Author (Author)

in views result, for each row, error appears:

Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 179 of /home/u5444/domains/medstream.krypton.vps-private.net/includes/entity.inc).
Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 356 of /home/u5444/domains/medstream.krypton.vps-private.net/includes/entity.inc).

Comments

dawehner’s picture

Status: Active » Postponed (maintainer needs more info)
StatusFileSize
new3.58 KB

Mh, as always i try to reproduce problems, so it's possible to fix them later.

Here is an export i tryed to play with, though i didn't got an error. What about providing an export for us? Maybe you text is missing one little detail here.

This kind of errors happens if $entity_load_multiple is called with wrong parameters.

dgastudio’s picture

StatusFileSize
new6.26 KB

sorry, attached the export.

also, can you please add comment author if comment is from not registered user?

i have imported your view, the result is the same

http://medstream.krypton.vps-private.net/testhh

dawehner’s picture

I'm wondering whether you have something like realname module installed? By just using this view i don't get an error like that, even every comment is created by uid = 0 etc.

Here is a short guess: your comment/node_comment_statistics table contains entries for a uid "" but actually it should be "0" so if something is triggering user_load, for example realname the notice get's fired.

It would be somehow helpful if you create a backtrace from that. Therefore you can use something like this code in "includes/entity.inc"

if (!is_array($ids)) {
  dsm(views_trace());
}
...
$passed_ids = !empty($ids) ? array_flip($ids) : FALSE;

I hope this gives enough information to tell a bit more about this problem.

dgastudio’s picture

Hi!

first of all, thank you for help me!

no, i don't have realname installed.

I have added your code to entity.inc. No result.

so, i have changed it to dprint_r($ids);

the result is:

<pre>Array
(
    [0] =&gt; 
)
</pre><pre>Array
(
    [0] =&gt; 
)
</pre><pre>Array
(
    [0] =&gt; 
)
</pre><pre>Array
(
    [0] =&gt; 
)
</pre><pre>Array
(
    [0] =&gt; 
)
</pre><pre>Array
(
    [0] =&gt; 
)
</pre><pre>Array
(
    [0] =&gt; 
)
</pre><pre>Array
(
    [0] =&gt; 
)
</pre><pre>Array
(
    [0] =&gt; 
)
</pre><pre>Array
(
    [0] =&gt; 
)
</pre>

http://medstream.krypton.vps-private.net/testhh

dgastudio’s picture

StatusFileSize
new3.33 KB

attached export of one of my comments from phpymadmin.

dgastudio’s picture

i think i have found the problem

i'm displaying, for example, 10 nodes in views.
5 nodes have comment (relationship "last comment" works)

but other 5, no, so, this is becouse id's array es empty and can't be flipped.

can you check it please?

thank you.

esmerel’s picture

Status: Postponed (maintainer needs more info) » Active
dsutter’s picture

Title: Warning: array_flip() [function.array-flip]: » Warning: array_flip() Missing field item
Issue summary: View changes
StatusFileSize
new809 bytes

I am getting
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 175 / Warning: array_key_exists() expects parameter 2 to be array, null given in theme_image_formatter() (line 615

Solved with this patch, which throws a watchdog notice instead of trying to render the missing item.

dsutter’s picture

Status: Active » Needs review
joseph.olstad’s picture

Status: Needs review » Reviewed & tested by the community

Patch 8 is really helpful, we were bouncing around this array_flip issue for months internally wondering where or how to fix this. Lo and behold, patch 8 to the rescue. Now our drupal admins will no longer have to scratch their heads with annoying array_flip errors caused by content with missing fields. Instead of an annoying and confusing , almost meaningless array_flip message, patch 8 prevents an array_flip by stopping the rendering process for empty field items. Rather than array_flip, a helpful watchdog message is provided that provides information about a node or entity that has a field with an empty or missing item. So now our drupal admins can solve the issue themselves by fixing the content that was causing the issue.

Great job @dsutter , 5 years after this issue was openned, you came up with an elegant solution! Great idea.

After reviewing the patch a bit more, we might want to add some validation for the entity_type first before assuming a node id (nid) and then add support for fid and mlid , tid , etc. Views can display many types of content more than just nid.

I'll see if I can whip up something to make this great patch even better.

joseph.olstad’s picture

Assigned: Unassigned » joseph.olstad
Status: Reviewed & tested by the community » Needs review
joseph.olstad’s picture

StatusFileSize
new1.11 KB

quick change so that all entity types are handled without notices. Not all entity types have nids

removed tab spacing , replaced with spaces
formatted for 80 columns as per code sniffer.

dsutter’s picture

StatusFileSize
new1.13 KB

Update to patch #12 to deal with yet another watchdog warning (array_key_exists() expects parameter 2 to be array).

joseph.olstad’s picture

StatusFileSize
new1.08 KB
joseph.olstad’s picture

new patch comming

Status: Needs review » Needs work

The last submitted patch, 14: views-array_flip_missing_field_item-1511392-14.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

joseph.olstad’s picture

ok, Duncans' patch 13 is the way to go.
we need to check that the array_key_exists because isset when the array element value is null would return false (meaning the test would fail) but we want to know when it's empty and empty on a null returns true

so therefore , logic stays as-is.

joseph.olstad’s picture

Status: Needs work » Needs review
joseph.olstad’s picture

Assigned: joseph.olstad » Unassigned
Issue summary: View changes
joseph.olstad’s picture

Issue summary: View changes
chris matthews’s picture

The patch in #13 applied cleanly to the latest views 7.x-3.x-dev.