Haven't quite tracked down the root cause of this yet, but the short version is:

1. select list with term options passed node->term_field[LANGUAGE_NONE][0] = array('tid' => NULL)
2. This gets saved into the db as array('tid' => 0) (in this case mongo).
3. If you then load and save the node, taxonomy_field_validate() evaluates 0 == 'autocreate' as TRUE
4. Then it tries to save a term with no vid or name and errors abound.

This took a long time to track down, not least because taxonomy_field_validate() runs on any taxonomy field, not just autocomplete.

Comments

Status: Needs review » Needs work

The last submitted patch, term.patch, failed testing.

catch’s picture

Title: taxonomy_field_validate() tries to create invalid terms » 'autocreate' == 0
Status: Needs work » Needs review
StatusFileSize
new3.16 KB

this shows up in a few more places, not looked at the test failures yet.

catch’s picture

Status: Needs review » Closed (won't fix)

99% sure this is due to a bad patch to taxonomy_field_is_empty() which was applied from months ago.

ezheidtmann’s picture

Status: Closed (won't fix) » Needs review

A year later, I am experiencing a similar issue, resolved by this patch. I am programmatically creating nodes with term reference fields, and if I have no terms, then a term with tid = 0 ends up in taxonomy_field_presave(), and, as you say, errors abound.

I have applied your patch and all is well again. Can this be included in a future release? A stricter comparison can't hurt in this context, can it?

xjm’s picture

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

Aha! I think I've encountered this before as well.

xjm’s picture

Status: Needs review » Needs work
Issue tags: +Novice

Well obviously it needs a reroll, but yeah.

Tagging as novice to reroll.

aaron.r.carlton’s picture

Status: Needs work » Needs review
StatusFileSize
new3.21 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch, term_reroll-873722-7.patch, failed testing.

aaron.r.carlton’s picture

Status: Needs work » Needs review
StatusFileSize
new3.21 KB

Rolled of master, not 8.x. Trying again :)

Status: Needs review » Needs work

The last submitted patch, term_reroll-873722-9.patch, failed testing.

aaron.r.carlton’s picture

Status: Needs work » Needs review
StatusFileSize
new3.23 KB

Git environment is screwy. Trying again :/

xjm’s picture

Issue tags: -Novice

That looks good. Thanks @aaron.r.carlton!

fgm’s picture

#11: term_reroll-873722-11.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, term_reroll-873722-11.patch, failed testing.

xjm’s picture

Issue tags: +Novice
damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new3.24 KB

Re-rolled patch and tested. Should apply cleanly now.

xjm’s picture

Issue tags: -Novice +Needs tests

Thanks @damiankloip; that looks good.

I wonder if we can create a test with the functional bug described in #4?

ezheidtmann’s picture

Hi xjm, I don't have a proper test environment set up yet, but here's the code that triggers the bug.

function autocreatebug_run() {
  $node = new stdClass();
  $node->type = "article";
  node_object_prepare($node);
  $node->uid = 1;
  $node->language = LANGUAGE_NONE;

  $node->title = "This will trigger the bug!";
  $node->field_tags[$node->language][0]["tid"] = 0;

  node_submit($node);
  node_save($node);
}

Before applying the patch, I get these errors:

$ drush ev 'autocreatebug_run()'
array_flip(): Can only flip STRING and INTEGER values!                     [warning]
entity.controller.inc:182
WD node: EntityMalformedException: Missing bundle property on entity of    [error]
type taxonomy_term. in entity_extract_ids() (line 148 of
/home/evanh/.web/autocreate/modules/entity/entity.module).
WD php: EntityMalformedException: Missing bundle property on entity of type[error]
taxonomy_term. in entity_extract_ids() (line 148 of
/home/evanh/.web/autocreate/modules/entity/entity.module).
EntityMalformedException: Missing bundle property on entity of type taxonomy_term. in entity_extract_ids() (line 148 of /home/evanh/.web/autocreate/modules/entity/entity.module).
Drush command terminated abnormally due to an unrecoverable error.         [error]

After applying the patch, there are no errors. An row appears in {taxonomy_index} relating the new node to tid 0, but there is no row for tid 0 in {taxonomy_term_data} and the UI shows no terms for the new node. An edit -> save cycle on the UI will remove the row in {taxonomy_index}. I am open to suggestion if my test code breaks an API expectation.

The 8.x branch has drifted somewhat since the patch in #16; attached is a re-roll that applies cleanly today.

If you (or anyone) can point me to the proper test-bench setup instructions, I will create a proper test.

Status: Needs review » Needs work

The last submitted patch, 0001-Fix-errors-for-tid-0-in-term-reference-field.patch, failed testing.

ezheidtmann’s picture

StatusFileSize
new3.21 KB

Darn that testbot. I can't figure out why the old patch didn't apply. Now I've followed the instructions from http://drupal.org/node/707484 and used git diff instead of git format-patch ...

ezheidtmann’s picture

Sorry, it's all me. I was using a months-old version of the 8.x branch. My bad. Please hold while I get back up to speed -- the patch in #16 no longer applies and my PHP version is too old to test anything. This patch is, thus, untested.

ezheidtmann’s picture

Status: Needs work » Needs review
StatusFileSize
new3.99 KB

I'm back, with the real latest 8.x. I have a silly-short module with this code:

function autocreatebug_run() {
  $node = new stdClass();
  $node->type = "article";
  node_object_prepare($node);
  $node->uid = 1;
  $node->language = 'und';

  $node->title = "This will trigger the bug!";
  $node->field_tags[$node->language][0]["tid"] = 0;

  node_submit($node);
  node_save($node);
}

In 8.x I can trigger the bug like this:

$ drush ev 'autocreatebug_run()'
array_flip(): Can only flip STRING and INTEGER values! entity.controller.inc:186                        [warning]
WD taxonomy_term: EntityMalformedException: Missing bundle property on entity of type taxonomy_term. in [error]
entity_extract_ids() (line 149 of /home/evanh/.web/autocreate/core/modules/entity/entity.module).
WD node: EntityStorageException: Missing bundle property on entity of type taxonomy_term. in            [error]
EntityDatabaseStorageController->save() (line 541 of
/home/evanh/.web/autocreate/core/modules/entity/entity.controller.inc).
WD php: EntityStorageException: Missing bundle property on entity of type taxonomy_term. in             [error]
EntityDatabaseStorageController->save() (line 541 of
/home/evanh/.web/autocreate/core/modules/entity/entity.controller.inc).
EntityStorageException: Missing bundle property on entity of type taxonomy_term. in EntityDatabaseStorageController->save() (line 541 of /home/evanh/.web/autocreate/core/modules/entity/entity.controller.inc).
Drush command terminated abnormally due to an unrecoverable error.  

And the attached patch allows the above command to run without errors.

I don't have the automated test environment set up yet, so I don't have a patch for the test yet.

naxoc’s picture

Issue tags: -Needs tests

#22: autocreate.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +Needs tests

The last submitted patch, autocreate.patch, failed testing.

xjm’s picture

Issue tags: +Needs reroll
naxoc’s picture

StatusFileSize
new3.98 KB

Reroll. I'll see if I can cook up a test for this soon.

xjm’s picture

Status: Needs work » Needs review

Sending to the bot.

star-szr’s picture

Issue tags: -Needs reroll

Removing reroll tag.

dawehner’s picture

Issue tags: -Needs tests

#26: autocreate-term-873722-26.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +Needs tests

The last submitted patch, autocreate-term-873722-26.patch, failed testing.

damiankloip’s picture

Is this issue even still relevant now? I think a lot of this code has changed. On top of that we have the TypedData api.

jibran’s picture

Version: 8.0.x-dev » 7.x-dev
Issue summary: View changes
Status: Needs work » Active

Well after #1847596: Remove Taxonomy term reference field in favor of Entity reference the auto create api is yet again changed as it's using ER field now. They handle autocreate complete differently. See \Drupal\Core\Entity\Element\EntityAutocomplete::validateEntityAutocomplete() for details.
But this is still an issue in D7 so moving back to D7 issue queue.

stefan.r’s picture

Issue tags: +Needs reroll, +Novice, +Dublin2016

Tagging Novice for the reroll (of #16) and tests

MaskyS’s picture

Assigned: Unassigned » MaskyS

Let's try and re-roll this.

MaskyS’s picture

Status: Active » Needs review
StatusFileSize
new3.51 KB

Re-rolled patch from #16. [Patch tested on git and simplytest.me]
Please review :)

Status: Needs review » Needs work

The last submitted patch, 35: rerolled-patch-873722-35.patch, failed testing.

MaskyS’s picture

Status: Needs work » Needs review

Could have failed because I edited the comment during the last phases of the test...

MaskyS’s picture

As expected, the second test passed.

Status: Needs review » Needs work

The last submitted patch, 35: rerolled-patch-873722-35.patch, failed testing.

MaskyS’s picture

Status: Needs work » Needs review

Patch works, but apparently, fails on PHP 5.3 tests. Could be an issue with the test bot.

Status: Needs review » Needs work

The last submitted patch, 35: rerolled-patch-873722-35.patch, failed testing.

MaskyS’s picture

Status: Needs work » Reviewed & tested by the community

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 35: rerolled-patch-873722-35.patch, failed testing.

MaskyS’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Dublin2016

Ugh, stop it testbot.

MaskyS’s picture

Assigned: MaskyS » Unassigned
Issue tags: -Needs reroll

Yay, test passed. I'm unassigning so that someone can review and commit this.

MaskyS’s picture

Status: Reviewed & tested by the community » Needs review

Review please?

poker10’s picture

StatusFileSize
new1008 bytes
new4.93 KB

The patch #35 looks good. Adding a simple test to show the failure with tid = 0.

The last submitted patch, 47: 873722-47_test-only.patch, failed testing. View results

mcdruid’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests, -Novice +RTBM

Thanks for adding the test.

Looks good to me!

  • poker10 committed cc05fac on 7.x
    Issue #873722 by ezheidtmann, aaron.r.carlton, catch, poker10, MaskyS,...
poker10’s picture

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

Thanks all who contributed! Commited and pushed to 7.x.

Status: Fixed » Closed (fixed)

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