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?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mstrelan’s picture

Title: Array flip error » Array flip error when a taxonomy term field has a NULL value
Version: 7.12 » 8.x-dev
Component: entity system » taxonomy.module
Category: support » bug
Status: Active » Needs review
FileSize
792 bytes

I get the same error. I found that some taxonomy term fields have a NULL value in the database for my "tags" field. For example:

mysql> SELECT * FROM  field_data_field_tags WHERE field_tags_tid IS NULL;
+-------------+--------+---------+-----------+-------------+----------+-------+----------------+
| entity_type | bundle | deleted | entity_id | revision_id | language | delta | field_tags_tid |
+-------------+--------+---------+-----------+-------------+----------+-------+----------------+
| node        | story  |       0 |       264 |         264 | und      |     0 |           NULL |
| node        | story  |       0 |       453 |         453 | und      |     0 |           NULL |
| node        | story  |       0 |       528 |         528 | und      |     0 |           NULL |
| node        | story  |       0 |      1070 |        1070 | und      |     0 |           NULL |
| node        | story  |       0 |      1117 |        1117 | und      |     0 |           NULL |
| node        | story  |       0 |      2368 |        2368 | und      |     0 |           NULL |
| node        | story  |       0 |      2891 |        2892 | und      |     0 |           NULL |
| node        | story  |       0 |      4237 |        4238 | und      |     0 |           NULL |
| node        | story  |       0 |      5718 |        5731 | und      |     0 |           NULL |
| node        | story  |       0 |      6588 |        6601 | und      |     0 |           NULL |
| node        | story  |       0 |      7928 |        7946 | und      |     0 |           NULL |
| node        | story  |       0 |      9041 |        9059 | und      |     0 |           NULL |
| node        | story  |       0 |      9137 |        9155 | und      |     0 |           NULL |
| node        | story  |       0 |      9138 |        9156 | und      |     0 |           NULL |
| node        | story  |       0 |      9168 |        9186 | und      |     0 |           NULL |
+-------------+--------+---------+-----------+-------------+----------+-------+----------------+
15 rows in set (0.00 sec)

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()?

mstrelan’s picture

I'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.

kscheirer’s picture

Status: Needs review » Needs work

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

iamEAP’s picture

Issue tags: +Needs backport to D7

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

iamEAP’s picture

Status: Needs work » Needs review
FileSize
630 bytes

Patch for D8.

iamEAP’s picture

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

iamEAP’s picture

Status: Needs review » Needs work
iamEAP’s picture

iamEAP’s picture

Issue summary: View changes

Updating issue to use issue summary template.

iamEAP’s picture

iamEAP’s picture

Issue summary: View changes

Getting rid of user specific directory in the error string.

Status: Needs review » Needs work
divesh.kumar’s picture

Status: Needs work » Needs review
FileSize
998 bytes

Patch upated.

jhedstrom’s picture

Status: Needs review » Needs work

Still needs tests. Also, the patch in #13 isn't valid for D8, see https://www.drupal.org/node/1805846

nlisgo’s picture

Assigned: Unassigned » nlisgo

I'm going to take this one on.

nlisgo’s picture

I'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

nlisgo’s picture

Assigned: nlisgo » Unassigned
jibran’s picture

Version: 8.0.x-dev » 7.x-dev

After #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.

David_Rothstein’s picture

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

poker10’s picture

Related issues: +#873722: 'autocreate' == 0

I think this issue can be related to: #873722: 'autocreate' == 0 . Will see if this would be reproducible after that related issue will be fixed.

poker10’s picture

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

Warning: array_flip(): Can only flip string and integer values, entry skipped in DrupalDefaultEntityController->load() (line 175 of /includes/entity.inc).

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):

unset($items[$id][$delta]);
$rekey = TRUE;

Adding a patch with this array_filter() approach and also added the test with the failure (no interdiff as this is completely different approach).

The last submitted patch, 23: 1470236-23_test-only.patch, failed testing. View results

poker10’s picture

Issue tags: +Pending Drupal 7 commit

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

mcdruid’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Pending Drupal 7 commit +RTBM

I 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 to taxonomy_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.

  • poker10 committed f1199f88 on 7.x
    Issue #1470236 by iamEAP, mstrelan, poker10, divesh.kumar: Array flip...
poker10’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: -RTBM

Committed, thanks everyone!

Status: Fixed » Closed (fixed)

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