Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I had several nodes with an optional taxonomy vocabulary, a few of which had no selection from that vocabulary. I later made the vocabulary required for those nodes' content types, and I then got errors that are probably because of this.
This error usually, not always, showed up after a cron run: Invalid argument supplied for foreach() ... taxonomy.module on line 1214
Seems like the taxonomy module should warn of blank taxonomy entries before allowing a vocab to become mandatory?
Comment | File | Size | Author |
---|---|---|---|
#17 | taxonomy_node_update_index.patch | 666 bytes | dman |
#15 | taxonomy_node_update_index.patch | 642 bytes | dman |
#12 | taxonomy_node_update_index.patch | 650 bytes | dman |
Comments
Comment #1
Aren Cambre CreditAttribution: Aren Cambre commentedPossibly related:
Comment #2
dman CreditAttribution: dman commentedIf I were coding it, I'd just make the code more robust, and shut it up.
Comment #3
Aren Cambre CreditAttribution: Aren Cambre commentedComment #4
thollowood CreditAttribution: thollowood commentedI can confirm that substituting this code
foreach ((array)$node->taxonomy as $term) {
solved the following problem: Invalid argument supplied for foreach() in /home/mydir/mysite.org/modules/taxonomy/taxonomy.module on line 1226.
Comment #5
xjmSame as in #4 here. We should really be checking if something is empty before using it as an array. The typecasting works fine in this case, though.
Comment #6
jan_v CreditAttribution: jan_v commentedThe same from #4 applies for me. Although i usually never change code in drupal core modules, i've made an exception for this one, because it seriously pissed me off that every time cron was run, there was a big errorlist of "Invalid argument supplied for foreach()" .
I hope this minor change is included in the next taxonomy update (if there's one on the way).
thanks dman.
Comment #7
sethcohn CreditAttribution: sethcohn commented+1... just got bit by this... there should be a check here, for non-array passed for whatever reason, to at least fail gracefully.
Adding (array) solves this cleanly if not beautifully.
Comment #8
Aren Cambre CreditAttribution: Aren Cambre commentedThis solution bothers me because it still leaves the data in an inconsistent state. Also, does this code suppress errors that we ought to see?
Comment #9
jan_v CreditAttribution: jan_v commentedIt's true what you're saying. The data that doesn't pass an array as all the data is supposed to do, still won't pass an array on the next occasion.
Although i don't think that by adding the (array) to it, we will miss out on any errors or warnings because it was simply not catched.
A nice official fix for this problem would be better of course.
Comment #10
dman CreditAttribution: dman commentedIt comes down to programmers pragmatism.
*I* believe that an API that is an advertised interface with external unregulated code should be able to protect itself against incorrect input or at least identify where the problem is coming from so it can be fixed. You call PHP core functions with the wrong number or type of arguments, then it tells you what you did wrong. We can do the same.
The Drupal API (in D6) does not have any of those safeguards built in (assertions etc) anywhere and has tended to fall into the "don't call API functions unless the input is perfect and we'll hope that core won't die" sort of approach.
Building code where you can assume that nobody ever makes mistakes is nice, but I find it fragile.
*I* find it more interesting to built fault-tolerant code that helps bad coders find out what they are doing wrong (notices) but doesn't cause failure if there is a small problem.
In this case (I've seen it, or something very similar elsewhere) it's really hard to find out where in the chain this admittedly incorrect value was added to the data. It would that some error-checking overhead somewhere else in the chain to really see what was going wrong.
Here's another issue where taxonomy processing was unable to deal with slightly unexpected datatypes being added to an input array
So ... Where I get the chance, I add at least a data type check to places where I expect that someone, somewhere may accidentally give the wrong type of input to an API call. Or if that fails, just do something that makes it no longer a bad error if they do.
Comment #11
Aren Cambre CreditAttribution: Aren Cambre commentedGiven #9 and #10, the proposed patch appears to be a temporary workaround. Putting back to active since a complete patch needs to be presented.
Comment #12
dman CreditAttribution: dman commentedin this case where this function is small, called from only one place, and no harm can come from casting it like this, I don't think that there is anything wrong with the first approach - it's probably more efficient than anything else.
But for a more normal fix, this attached path will also do what we want, in the caller, in a tidy, correct way.
Personally I'd still favor making the function more robust and fault-tolerant, but this alternative way ensures that the fault doesn't happen in the first place. Also good.
Comment #13
dman CreditAttribution: dman commentedbot-test it, I guess.
Comment #15
dman CreditAttribution: dman commentedMeh. roll the patch from the right directory this time.
Comment #17
dman CreditAttribution: dman commentedI haven't had my coffee yet. Sorry bot, THIS directory I mean.
Comment #18
dman CreditAttribution: dman commentedComment #20
MustangGB CreditAttribution: MustangGB commentedComment #21
Agileware CreditAttribution: Agileware commentedSubscribe.
Comment #22
Aren Cambre CreditAttribution: Aren Cambre commentedDoes this problem exist in D7?
Comment #23
dman CreditAttribution: dman commentedI was unable to replicate the error in d7.
The whole term tagging process has been turned inside out. This issue would have been unlikely to persist.
Adding a mandatory vocabulary to existing content does NOT cause this problem in d7.
Due to lack of priority, a fix like this is unlikely to be bothered with for d6 it seems
Comment #24
wxman CreditAttribution: wxman commentedI don't know if it would help anyone, but I was getting the error on every cron run after updating to Pressflow 6.19.92. I tried the simple fix in #2, and it hasn't come back yet.
Comment #25
Agileware CreditAttribution: Agileware commentedIt is important that a fix for this bug be committed for Drupal 6. Re-opening.
Comment #26
rootworkThis problem was affecting me as well; I used the solution in #2 and it solved it.
I would also like to see this simple bug with a simple fix get committed in a future D6 update. Among other things, it can generate a page full of scary looking errors every time cron is run, not something that end users will think is a minor bug.
Comment #27
c-c-m CreditAttribution: c-c-m commentedSubscribing what rootwork says in #26
Comment #28
ehowland CreditAttribution: ehowland commentedI also found the errors to go away when I used #2. I thought it would be good to find out which nodes were bad so I could fix them. I added code to dump the node if taxonomy was Null. Sure enough I got three dumps corresponding to my 3 previous errors. Surprising to me is that these are not really nodes, at least not yet. No nid or vid or even anything in the body?
They look like this:
The build modes may be coming from Display Suite which is running on this site.
Comment #29
xjmThere isn't a working 6.x patch in this issue, so setting NW.