Problem/Motivation
If a field associated with an entity points to a taxonomy term, and there happens to be no data in that field on that entity (i.e., the term tid is NULL), then taxonomy_field_formatter_prepare_view() attempts to load taxonomy term data for a NULL tid. In doing so, DrupalDefaultEntityController::load() attempts to array flip a NULL value, which is not allowed (only strings/ints are allowed), resulting in the following PHP warning and associated log message:
Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 177 of /path/to/drupal/includes/entity.inc).
Proposed resolution
The proposed resolution is a simple patch that performs a check for is_numeric() on the tid in taxonomy_field_formatter_prepare_view() before adding it to the list of tids to be loaded. The rationale here is that taxonomy_term_load() does the same check before calling taxonomy_term_load_multiple(), so we should be performing the same check in taxonomy_field_formatter_prepare_view().
Remaining tasks
- The proposed patch should get at least a couple of non-bot reviews before committing. It's relatively small though, and passes tests.
- Afterward, the patch will need to be applied to Drupal 7 as well.
User interface changes
None.
API changes
None.
Original report by POVYLAZZZ
I was working on my site then on node creation pages I started seeing this error:
Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of /var/www/p/includes/entity.inc).
What could have caused it?
I read that FileField Sources and Page titles could be the reason of this error but I have never used the first and have not yet installed Page titles.
What type of module could be to blame?
Comments
Comment #1
mstrelan CreditAttribution: mstrelan commentedI get the same error. I found that some taxonomy term fields have a NULL value in the database for my "tags" field. For example:
What is interesting about this data is that I used node_convert to convert all my story nodes to article nodes, and field_tags never existed for the story content type, so I guess this might be a node_convert issue. If I edit and save the node without making changes the row in this table is removed and the error goes away.
I wonder if it's worth checking that the tid value isn't NULL in
taxonomy_field_formatter_prepare_view()
?Comment #2
mstrelan CreditAttribution: mstrelan commentedI've just found #1102570: array_flip() [function.array-flip] issue in DrupalDefaultEntityController / entity.inc which is probably a more appropriate issue? Patch in #26 over there is a generic way to solve this.
Comment #3
kscheirerThat issue seems to talk only about D7. If this is still an issue in D8, then either that issue needs to be moved there, or this issue needs a patch to fix it in D8.
Just fixing up 1 view does not seem like the right solution, unless that's the only place its a problem in D8.
Comment #4
iamEAP CreditAttribution: iamEAP commentedFound this on my installation. Looks like taxonomy_field_formatter_prepare_view() doesn't properly check to make sure the tids it attempts to load are numeric; you'll see that taxonomy_term_load does do a check for this before it calls taxonomy_term_load_multiple. I think it makes sense to do this check here too, since we're calling that function as well.
This affects D8 and D7.
The patch in #1 may be wider reaching than this needs to be. This could be an extremely simple one-liner.
Comment #5
iamEAP CreditAttribution: iamEAP commentedPatch for D8.
Comment #6
iamEAP CreditAttribution: iamEAP commentedActually, reading through the code and documentation more, the patch in #1 was right in unsetting the term from $items. However, I think it's worth keeping with the use of is_numeric since that's what's used in taxonomy_term_load().
New patch for review.
Comment #7
iamEAP CreditAttribution: iamEAP commentedFor Drupal 7.
Comment #9
iamEAP CreditAttribution: iamEAP commentedTests are awesome.
Comment #9.0
iamEAP CreditAttribution: iamEAP commentedUpdating issue to use issue summary template.
Comment #10
iamEAP CreditAttribution: iamEAP commented#9: d8-array_flip_taxonomy_field_formatter_prepare_view-1470236-9.patch queued for re-testing.
Comment #10.0
iamEAP CreditAttribution: iamEAP commentedGetting rid of user specific directory in the error string.
Comment #13
divesh.kumar CreditAttribution: divesh.kumar commentedPatch upated.
Comment #15
andypostComment #16
jhedstromStill needs tests. Also, the patch in #13 isn't valid for D8, see https://www.drupal.org/node/1805846
Comment #17
nlisgo CreditAttribution: nlisgo commentedI'm going to take this one on.
Comment #18
nlisgo CreditAttribution: nlisgo commentedI'm first going to attempt to establish whether this is still an issue in D8.
These are the steps I have followed on a clean install of 8.0.x:
Create new article node (/node/add/article) and leave Tags field empty and save.
Result: no entry in the node__field_tags table.
Edit node (/node/1/edit) and add a tag 'Tag one' and save.
Result: 1 entry in the node__field_tags table.
Delete the 'Tag one' term (/admin/structure/taxonomy/manage/tags/overview).
Visit node/1
Result: Fatal error: Call to a member function getName() on a non-object in /vagrant_sites/drupal8.local/web/core/modules/taxonomy/src/Plugin/Field/FieldFormatter/LinkFormatter.php on line 45
and
1 entry remains in the node__field_tags table with a value in field_tags_target_id column.
I believe this is a different issue which I will report if I cannot find it in the issue queue. But I can not reproduce this bug.
Any help? @jhedstrom @iamEAP
Comment #19
nlisgo CreditAttribution: nlisgo commentedComment #20
jibranAfter #1847596: Remove Taxonomy term reference field in favor of Entity reference taxonomy term field is gone. This is no longer a bug in D8. Moving back to D7.
Comment #21
David_Rothstein CreditAttribution: David_Rothstein as a volunteer commentedOne place where I've seen this bug is if the entity that has the taxonomy term field attached has gone through a form rebuild (e.g. after a validation error) and the form was submitted with no terms selected - then https://api.drupal.org/api/drupal/modules!field!field.default.inc/functi... winds up taking empty NULL values from $form_state['values'] and polluting the entity with those values. So that is how you wind up with a NULL entry for the 'tid'.
So in that case, at least, it seems like a deeper bug with the field system or form API, nothing specific to taxonomy terms. I'm not sure if that still affects Drupal 8 or not.
Comment #22
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedI think this issue can be related to: #873722: 'autocreate' == 0 . Will see if this would be reproducible after that related issue will be fixed.
Comment #23
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedThis was not fixed by the mentioned issue. The array_flip warning is still present if you have a NULL value in the TID column in the database and you open the taxonomy term detail.
As it was mentioned, problem is in the
taxonomy_field_formatter_prepare_view()
function with this loading:$terms = taxonomy_term_load_multiple($tids);
if the$tids
array contains NULLs.I think this can be solved most easily by adding an
array_filter()
to the$tids
array - this will filter out NULLs. Then the NULLs will be removed also from the$items
array few lines below (so do not need to duplicate the same code here, like the patch #1 was doing):Adding a patch with this
array_filter()
approach and also added the test with the failure (no interdiff as this is completely different approach).Comment #25
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedWe have used similar fix in this issue: #3006007: Don't try to load the user picture if it's not set. Adding a tag for the other maintainer review.
Comment #26
mcdruidI could go either way with whether we avoid putting the entries into the
$tids
array in the preceding loop, or filter them out when passing the list totaxonomy_term_load_multiple()
per the last patch.I don't think there's much difference, but this approach is simple.
I can't see how it would cause problems as presumably
taxonomy_term_load_multiple()
was never going to return anything for the null elements.Thanks for adding tests!
I think this looks good and is ready to commit.
Comment #28
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedCommitted, thanks everyone!