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).
| Comment | File | Size | Author |
|---|---|---|---|
| #26 | array_flip_error-2476821-13.patch | 1.59 KB | znerol |
| #26 | array_flip_error-2476821-13-TEST-ONLY.patch | 1.04 KB | znerol |
| #19 | array_flip-warning-entity-inc.patch | 914 bytes | jmouse888 |
| #13 | array_flip_error-2476821-13.patch | 1.59 KB | znerol |
| #13 | array_flip_error-2476821-13-TEST-ONLY.patch | 1.04 KB | znerol |
Comments
Comment #1
ladybug_3777 commentedThis 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.
Comment #2
znerol commentedThanks for the bug report. That particular array flip error is known to be triggered if invalid parameters are passed into
node_load(orentity_load). One example isnode_load(TRUE)ornode_load(FALSE).However, Taxonomy Deep Index does not use neither
node_loadnorentity_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.Comment #3
ladybug_3777 commentedFrom 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.Comment #4
ladybug_3777 commentedTo 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
Comment #5
ladybug_3777 commentedAttached 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!)
Comment #6
ladybug_3777 commentedComment #7
znerol commentedThanks 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_ancestorsproperty is accessed, the entity_metadata_book_get_properties() function is used to extract and load all parent nodes by examiningbook['plid'](parent link id). It stops as soon as it fails to retrieve aplid.This code assumes that if a content is not part of the book hierarchy, then its
plidwill be empty. However, that is not the case. The book module sets theplidto-1instead. As a resultentity_metadata_book_get_properties()returnsarray(NULL)for thebook_ancestorsproperty for content which is not part of a book.Moving this issue over to the Entity API queue.
Comment #8
znerol commentedLet's try that.
Comment #9
znerol commentedOh crap, the
TEST-ONLYpatch is supposed to fail.Comment #10
znerol commentedLet's try with a proper test.
Comment #13
znerol commentedAt least the test now triggers the array flip errors. Removing bogus assertion, and we should be fine.
Comment #15
ladybug_3777 commentedAwesome, 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.
Comment #16
znerol commentedIt still feels weird to check explicitly for
-1. Maybe it would be better to just attempt tonode_load($link['nid'])first and only add thenidto$ancestorsif the node was loaded successfully. This would be more robust because it also would protect from dangling ancestor links (if that ever happens).Comment #17
ladybug_3777 commentedI 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?
Comment #18
ladybug_3777 commentedI was hoping someone else might be able to chime in on this issue. Anyone else have some information they could add?
THANKS in advance!
Comment #19
jmouse888 commentedTry this patch. I will submit here also.
https://www.drupal.org/node/2487462#comment-10040694
Comment #21
ladybug_3777 commentedjmouse888,
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!
Comment #22
ladybug_3777 commentedI'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???!
Comment #23
izmeez commentedI'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?
Comment #24
ladybug_3777 commentedizmeez ~ 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.
Comment #25
znerol commentedThe 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).Comment #26
znerol commentedRe-uploading the patches from #13 such that Entity API maintainers do not need to search through the comments.
Comment #28
joelpittetThis comes with tests and fixes the specific problem defined in the issue summary.
Comment #30
ladybug_3777 commented+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!!
Comment #31
leisurman commentedMy 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
Comment #33
fagoThanks, committed.