Problem/Motivation

When the book_ancestors property is accessed on content which is enabled for books, but is not placed into a book hierarchy, the following PHP warning is reported:

Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 175 of C:\inetpub\wwwroot\drupal_test\includes\entity.inc).

This is caused by entity_metadata_book_get_properties() which assumes that content which is not placed in the book hierarchy has an empty $book['plid']. However, that is not the case. Instead the book module sets plid=-1 for book content which does not have a parent - either because it is not in the hierarchy or because it is the toplevel book entry.

Proposed resolution

Remaining tasks

User interface changes

API changes

Original report in Taxonomy Deep Index
I've enabled this module and followed the instructions to bulk-update. However when I edit any node, after saving, I get the following error:

Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 175 of C:\inetpub\wwwroot\drupal_test\includes\entity.inc).

Comments

ladybug_3777’s picture

This seems to be related to using this module in conjunction with the Book module. Still looking into the issue and will likely come back with a patch for the problem.

znerol’s picture

Thanks for the bug report. That particular array flip error is known to be triggered if invalid parameters are passed into node_load (or entity_load). One example is node_load(TRUE) or node_load(FALSE).

However, Taxonomy Deep Index does not use neither node_load nor entity_load, therefore it is quite difficult for me to find the cause of this error. It would be fantastic if you could provide a stack-trace - or at least outline the steps necessary to reproduce it.

ladybug_3777’s picture

From what I've found so far, the problem exists when a content type is enabled to be included in a book, but a node is not actually added to a book. I'm going to strip down my version of Drupal so I only have the bare minimum of modules installed so I can ensure no other modules are contributing to the error.

From what I've found so far is that within the _taxonomy_deep_index_collect function the field name: "book_ancestors" is being passed in with a type of "list<node>". When checking the value of that field in _taxonomy_deep_index_wrapper_check the $value variable contains an array with [0]=NULL. I'm thinking this is the source of the array flip problem. Array flip cannot flip the 0 and NULL values. I think entity_load IS being called, it just isn't direct. It's called when this function is performed: $result[] = $field->tid->value(); and possibly also here: $value = $entity_wrapper->value();

I can circumvent the problem by adding "list<node>" to the type of excluded entity types, but that doesn't feel quite right (which is why I haven't posted a patch with that solution yet). I'm going to spend a little more time on it today and if I don't find a better solution, that will be what I do in a patch for now. But first I'd like to try a few more things first.

ladybug_3777’s picture

To recreate

Enable "Book" module
Add content of type "Book"
Make sure Book Outline says "Not in book" (default)
publish/save
You should see the warning message right after you save.

This will actually happen on all content types, regardless of if they are enabled to be used in a book outline or not.

Another test path:
Edit that same node (or create a new one) and this time select "create a new book" on the Book Outline tab
publish/save
Notice the error goes away

ladybug_3777’s picture

Attached patch adds list<node> to the list of exclude_types.

I'm not sure this is the BEST solution for the issue, but this works. Hopefully someone can review and see if there is a better way to tackle this. (Thanks in advance!)

ladybug_3777’s picture

Status: Active » Needs review
znerol’s picture

Project: Taxonomy Deep Index » Entity API
Component: Code » Core integration
Issue summary: View changes

Thanks for the fantastic research and the clear step-by-step directions, I can reproduce the problem now.

I think that this is an issue with the Entity API. When the book_ancestors property is accessed, the entity_metadata_book_get_properties() function is used to extract and load all parent nodes by examining book['plid'] (parent link id). It stops as soon as it fails to retrieve a plid.

This code assumes that if a content is not part of the book hierarchy, then its plid will be empty. However, that is not the case. The book module sets the plid to -1 instead. As a result entity_metadata_book_get_properties() returns array(NULL) for the book_ancestors property for content which is not part of a book.

Moving this issue over to the Entity API queue.

znerol’s picture

Let's try that.

znerol’s picture

Status: Needs review » Needs work

Oh crap, the TEST-ONLY patch is supposed to fail.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new1.08 KB
new1.64 KB

Let's try with a proper test.

The last submitted patch, 10: array_flip_error-2476821-10-TEST-ONLY.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 10: array_flip_error-2476821-10.patch, failed testing.

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new1.04 KB
new1.59 KB

At least the test now triggers the array flip errors. Removing bogus assertion, and we should be fine.

The last submitted patch, 13: array_flip_error-2476821-13-TEST-ONLY.patch, failed testing.

ladybug_3777’s picture

Awesome, thank you for taking the time to look into this more and moving it over to the correct project page. I'll give your patch a try and will let you know if I see anything strange pop up as a result.

znerol’s picture

It still feels weird to check explicitly for -1. Maybe it would be better to just attempt to node_load($link['nid']) first and only add the nid to $ancestors if the node was loaded successfully. This would be more robust because it also would protect from dangling ancestor links (if that ever happens).

ladybug_3777’s picture

I wish I was more knowledgeable on the core entity system so I could suggest the best approach! Maybe someone else will chime in with some thoughts?

ladybug_3777’s picture

I was hoping someone else might be able to chime in on this issue. Anyone else have some information they could add?

THANKS in advance!

jmouse888’s picture

StatusFileSize
new914 bytes

Try this patch. I will submit here also.

https://www.drupal.org/node/2487462#comment-10040694

Status: Needs review » Needs work

The last submitted patch, 19: array_flip-warning-entity-inc.patch, failed testing.

ladybug_3777’s picture

jmouse888,
I can confirm that your patch makes the error go away, but I can't claim to know enough about the inner workings of entity to know if this is the right approach or not. I appreciate another set of eyes taking a look at this though, thank you!

ladybug_3777’s picture

I'm totally stuck on this issue still. My old patch for this problem no longer applies to the newest dev version of taxonomy deep index. The file jmouse88 created no longer applies to the dev version of entity, and the patch applied on this other core page https://www.drupal.org/node/2487462 contains debug statements that I can't use either. ACK! Help???!

izmeez’s picture

I'm not currently working on this but intrigued by the comments here and in #2487462: array_flip() warning in entity.inc

Is this thread just a duplicate or is the patch in #13 and the comments in #16 helpful to reach a solution?

ladybug_3777’s picture

izmeez ~ I think the issue is a tangled web. Some think it's Entity APIs issue, some think it's Core's issue, others think it's an individual module problem. However none of the issues have been solved or declared as the "main problem".

I'm hoping a solution will shake out somewhere.

znerol’s picture

The patch in #13 still applies to Entity API and should resolve the issue. If it does not, then please let me know. Taxonomy Deep Index should not need a patch if this one is applied.

The patch in #19 is misleading: It does not resolve the issue but merely just ignores invalid arguments passed to entity_load. The correct approach is the one proposed by @cilefen in #2487462-21: array_flip() warning in entity.inc (i.e., notify the developer if the function is called in a bad way).

znerol’s picture

Status: Needs work » Needs review
StatusFileSize
new1.04 KB
new1.59 KB

Re-uploading the patches from #13 such that Entity API maintainers do not need to search through the comments.

The last submitted patch, 26: array_flip_error-2476821-13-TEST-ONLY.patch, failed testing.

joelpittet’s picture

Status: Needs review » Reviewed & tested by the community

This comes with tests and fixes the specific problem defined in the issue summary.

The last submitted patch, 5: taxonomy_deep_index-array_flip_list_node-2476821-5.patch, failed testing.

ladybug_3777’s picture

+1 for committing patch #13.

I was trying the most recent patch (#19) which I now see is the incorrect one to use. #13 applied properly and is working. Thanks znerol!!

leisurman’s picture

My array_flip error can from a default image in a cck field that was missing inside the cck field after a d6 to d7 upgrade
https://www.drupal.org/node/1102570#comment-10978037

  • fago committed 604072f on 7.x-1.x
    Issue #2476821 by znerol, ladybug_3777, jmouse888: Array flip errors...
fago’s picture

Title: Array Flip Error » Array flip errors when books are not part of a book hierarchy
Status: Reviewed & tested by the community » Fixed

Thanks, committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.