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
Comment #2
catchthis shows up in a few more places, not looked at the test failures yet.
Comment #3
catch99% sure this is due to a bad patch to taxonomy_field_is_empty() which was applied from months ago.
Comment #4
ezheidtmann commentedA 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?
Comment #5
xjmAha! I think I've encountered this before as well.
Comment #6
xjmWell obviously it needs a reroll, but yeah.
Tagging as novice to reroll.
Comment #7
aaron.r.carlton commentedRerolled.
Comment #9
aaron.r.carlton commentedRolled of master, not 8.x. Trying again :)
Comment #11
aaron.r.carlton commentedGit environment is screwy. Trying again :/
Comment #12
xjmThat looks good. Thanks @aaron.r.carlton!
Comment #13
fgm#11: term_reroll-873722-11.patch queued for re-testing.
Comment #15
xjmComment #16
damiankloip commentedRe-rolled patch and tested. Should apply cleanly now.
Comment #17
xjmThanks @damiankloip; that looks good.
I wonder if we can create a test with the functional bug described in #4?
Comment #18
ezheidtmann commentedHi xjm, I don't have a proper test environment set up yet, but here's the code that triggers the bug.
Before applying the patch, I get these errors:
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.
Comment #20
ezheidtmann commentedDarn 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 ...
Comment #21
ezheidtmann commentedSorry, 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.
Comment #22
ezheidtmann commentedI'm back, with the real latest 8.x. I have a silly-short module with this code:
In 8.x I can trigger the bug like this:
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.
Comment #23
naxoc commented#22: autocreate.patch queued for re-testing.
Comment #25
xjmComment #26
naxoc commentedReroll. I'll see if I can cook up a test for this soon.
Comment #27
xjmSending to the bot.
Comment #28
star-szrRemoving reroll tag.
Comment #29
dawehner#26: autocreate-term-873722-26.patch queued for re-testing.
Comment #31
damiankloip commentedIs this issue even still relevant now? I think a lot of this code has changed. On top of that we have the TypedData api.
Comment #32
jibranWell 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.
Comment #33
stefan.r commentedTagging Novice for the reroll (of #16) and tests
Comment #34
MaskyS commentedLet's try and re-roll this.
Comment #35
MaskyS commentedRe-rolled patch from #16. [Patch tested on git and simplytest.me]
Please review :)
Comment #37
MaskyS commentedCould have failed because I edited the comment during the last phases of the test...
Comment #38
MaskyS commentedAs expected, the second test passed.
Comment #40
MaskyS commentedPatch works, but apparently, fails on PHP 5.3 tests. Could be an issue with the test bot.
Comment #42
MaskyS commentedComment #44
MaskyS commentedUgh, stop it testbot.
Comment #45
MaskyS commentedYay, test passed. I'm unassigning so that someone can review and commit this.
Comment #46
MaskyS commentedReview please?
Comment #47
poker10 commentedThe patch #35 looks good. Adding a simple test to show the failure with tid = 0.
Comment #49
mcdruid commentedThanks for adding the test.
Looks good to me!
Comment #51
poker10 commentedThanks all who contributed! Commited and pushed to 7.x.