Invalid argument supplied for foreach() in taxonomy.module on line 1188

John T. Haller - May 17, 2008 - 16:28

After a messy Drupal 6 upgrade, I'm getting the following error when cron.php is run:

Invalid argument supplied for foreach() in /var/www/vhosts/[site]/httpdocs/modules/taxonomy/taxonomy.module on line 1188.

I've found similar errors in the Drupal forums and on the web, but none seem to fit my situation. Any thoughts?

Ugly but it works...

grobemo - May 21, 2008 - 18:52

John,

I've been having the same problem, along with similar problems relating to CCK. (See, e.g., http://drupal.org/node/237585 and the links from there to similar issues.)

In general, these warnings arise when an incomplete node object is passed into a function. In this case in particular, nodes are sometimes passed to taxonomy_node_update_index() without $node->taxonomy, so the function gets upset when it looks for that property.

The only way I know of to get rid of this problem (for now) is to hack core. Add this code to the beginning of taxonomy_node_update_index():

  if (empty($node->taxonomy)) {
    return;
  }

This exits the function if the node doesn't have the taxonomy property.

If you're using Drupal 6.2. (with taxonomy.module v. 1.414.2.1), your taxonomy_node_update_index() should now look like this:

function taxonomy_node_update_index(&$node) {
  if (empty($node->taxonomy)) {
    return;
  }
 
  $output = array();
  foreach ($node->taxonomy as $term) {
    $output[] = $term->name;
  }
  if (count($output)) {
    return '<strong>('. implode(', ', $output) .')</strong>';
  }
}

Maybe this is an issue for the serious developers to explore a bit?

Thank you! That

bradleywebhome - June 13, 2008 - 05:47

Thank you! That modification worked exactly as you said it would. My symptoms were the same and your resolution seems to be right on.

I'm giving this a go as

Rob T - June 26, 2008 - 19:15

I'm giving this a go as well. Thanks for the tip.

Nice :)

stefgosselin - June 27, 2008 - 06:02

Stumbled on this thread after an hour of debugging, I had already figured that drupal_cron_run timed out, followed the trail to the search indexer, and that led me to taxonomy module, choking on taxonomy_update_node_index, as noted above.

The posted fix works fine, I wish I'd of seen this thread BEFOREHAND. How about someone submit it as a patch, save others the pain of it all.
Anyhow, thanks David for the post, very much appreciated sir.

I have yet to see any problem, however complicated, which, when looked
at in the right way, did not become still more complicated.
-- Poul Anderson

Thank you :) An unbelieveably

Dinis - June 17, 2009 - 10:24

Thank you :)

An unbelieveably simple fix which put a work-around in place that solved an issue I've been fighting with for weeks.

As a slight aside, is there any way to actually see which cotent / node is triggering the error? Maybe even a bodge to echo the nid of the node just before exiting the fuction? My thinking is one could then list all the content with incomplete taxonomy properties, fix them, and remove the core patch when it's all complete.

I'll experiment myself a little later.

Kind regards,
Danielle

Thank You:)

loko - September 23, 2009 - 03:49

Thank You:)

-------------------------
Plastic Surgeries

PHP error level

JJ - September 23, 2009 - 04:32

Could you avoid the hack by changing the PHP error level? Just curious. Thanks.

warning:Invalid argument supplied for foreach() online no:1193

computer_jin - January 29, 2009 - 13:01

Hi every one ,
I am also having this same issue before many days and today i debub how can i solve this issue actually i was having error ....
warning: Invalid argument supplied for foreach() in \includes\form.inc on line 1193.
I am having this issue and when i debug and comment the code of checkboxes in my module than this error is resolved but i have to use this checkboxes ,check my code if i m missing some minor code and tell me how can i solve this ..

$form['payment_options']['business_payment_method']= array(
'#type' => 'checkboxes',
'#title'=> t('Payment Options'),
'#default_value' => variable_get('business_payment_method',$selected_payment),
'#options' => $payment_options,
);
I am using this tell me if you understand how can i solve this issue ...

Thanks in Advance

Azhar
4 Ace Technologies(www.4acetech.com)
karachi,Pakistan

D7 core patch:

pwolanin - May 19, 2009 - 22:42

---
Work: Acquia

Thankyou, thankyou, thankyou!

aharown07 - May 30, 2009 - 15:42

Sorry for all the emoting but I was up half the night trying to figure out if this error was serious or not and how to fix it... disabling modules, rerunning cron, etc., repeat, etc.

In may case, imported a bunch of nodes from wordpress (using wordpress import module) and also a bunch from vbulletin (using vbtodrupal module) and some of these apparently are not quite all intact w/respect to taxonomy so... but I'm guessing.

Anyway, this fix works great.

I would like to second this-

chrism2671 - June 7, 2009 - 19:24

I would like to second this- we have the same problems on nodes that were imported from vbulletin.

Instead of hacking core, how about fixing the nodes

skor - June 28, 2009 - 04:45

I'm getting this, after having changed some nodes from one type to another, using an sql command.

So I took the above code one step further, and did this:

  if (empty($node->taxonomy)) {
    $bad_nid = $node->nid;
    printf('Node %d is broken, ',$bad_nid);
    return;
  }

Then I went an ran cron.php and I get a list of the nodes that have a problem. I have about 35-40 of them, and I could go edit them manually.

Then I tried adding print_r (and dprint_r from the devel module) to take a look at what was different about those nodes. I'm thinking maybe just pick a node of similar type and copy its taxonomy structure over to each of these broken nodes.

Any thoughts on how to do that?

I ended up cleaning the data also

aharown07 - June 28, 2009 - 19:23

...sort of. I eventually abandoned the hack because the messed up nodes were causing other, even more serious, problems. So I didn't really clean up the data, I just flushed it by returning to a clean backup.

It would be great tool if someone could put skor's code, along w/the "what's different" functionality into a module of some kind. This would allow developers of import modules like wordpress to drupal and vbulletin to drupal to troubleshoot import issues and look for ways around them.

Very Strange

skor - July 2, 2009 - 04:36

using the devel module's dprint_r, here's what a the taxonomy structure looks like for a 'good' node on my site:

    [taxonomy] => Array
        (
            [1] => stdClass Object
                (
                    [tid] => 1
                    [vid] => 1
                    [name] => Site Config
                    [description] => Notes on the configuration of this site
                    [weight] => 4
                )

        )

Here's what it looks like for a 'broken' node:

    [taxonomy] => Array
        (
        )

So I edited one of the nodes that had an empty taxonomy array, entered a term, and saved it. No change to the structure. Not sure where it's getting saved if not there, but the term was saved somewhere.

Next, I tried dis-associating the node type and the vocabulary, but that didn't help. Then I re-assigned it again, hoping that would re-initialize all the taxonomy storage. Still no luck.

So does anyone know where I

skor - July 6, 2009 - 23:13

So does anyone know where I can find some documentation on where in the database the taxonomy terms are associated with a particular node? I'd like to see if I can figure out how to copy the taxonomy attributes of a good node over the attributes of a broken node.

taxonomy_node_get_terms

grobemo - July 7, 2009 - 01:39

I think the magic happens in taxonomy_node_get_terms when it gets called from taxonomy_nodeapi.

The table 'term_node' is the

gilgabar - July 7, 2009 - 03:19

The table 'term_node' is the one that associates terms with nodes. The table 'term_data' holds the data about each term.

I tried running the snippet to print a list of problem nodes and ended up with 'node 0 is broken' thirteen times. Anyone have any insight into what that might mean? I usually get thirteen instances of the foreach() message whenever cron is run, so that part isn't a mystery. I'm not really sure what to make of the node id of zero though.

Sorry, not sure about that. I

skor - July 10, 2009 - 07:01

Sorry, not sure about that.

I don't think there is a node zero. All my sites start at node #1.

My Solution for 6.13: associate "broken nodes" with term

crispy - July 9, 2009 - 17:52

Hello,

I tried the above solution and cron run displayed:

Node 1 is broken, Node 2 is broken, Node 3 is broken,

That's every node in my new site on 6.13. Everyone of those node is a sitenote content type.

I did not any taxnomy created for sitenotes. Once I associated each of the three nodes with a term, the errors went away.

Don't have time to research this further. IF anyone finds out more please post.

review:

I changed following

To change starting with line 1212 of taxonomy.module, drupal 6.13

function taxonomy_node_update_index(&$node) {
  $output = array();
  foreach ($node->taxonomy as $term) {
    $output[] = $term->name;
  }
  if (count($output)) {
    return '<strong>('. implode(', ', $output) .')</strong>';
  }
}

to this:

function taxonomy_node_update_index(&$node) {
  if (empty($node->taxonomy)) {
    $bad_nid = $node->nid;
    printf('Node %d is broken, ',$bad_nid);
    return;
  }

  $output = array();
  foreach ($node->taxonomy as $term) {
    $output[] = $term->name;
  }
  if (count($output)) {
    return '<strong>('. implode(', ', $output) .')</strong>';
  }
}

And then back again to original code once I figured having a term on my nodes solves the issue.

Mine are sitenotes also

skor - July 10, 2009 - 06:59

I had assumed that they were broken because I had originally entered them as admin blog posts, and then at some point changed their content type from blog to sitenote. Now I think I should go in and check if there are any broken node that didn't get converted.

Another option

skor - August 25, 2009 - 13:25

No time to investigate right now, but here's a code snippet that could be customized to find & fix the nodes:

http://drewish.com/node/139

Just posting here in case anyone else wants to take a look, or in case I get some time next month.

Another variation

mrb@drupal.org - July 10, 2009 - 08:55

I had the same 'Invalid argument supplied for foreach() ...' message described above, but when I added the debug lines to taxonomy_node_update_index, it just showed:

Node 0 is broken, Node 0 is broken,

The other symptom was a record in the search_dataset table with sid=0, but no corresponding record in the node table with nid=0.

By also adding a debug print at the start of _node_index_node in node.module, I could see which nodes were causing the errors. They seemed like problem nodes from a long way back, with no matching entries in node_revisions, so I was able to delete them, and the sid=0 record from search_dataset. It all seems ok now.

So the source of the problem in this case was corrupted nodes, made worse by the fact that in _node_index_node, there is no check to see if the first line's call to node_load is successful.

thanks

gilgabar - July 13, 2009 - 07:11

Thanks mrb, that solved the problem finding my own corrupt node 0 nodes.

stuck in same problem

abi - July 31, 2009 - 05:32

can you pls tell me how did you solve this working from last 5 hours on this issue.. need you r help .. pls reply back with all steps to took to solve this.

abi

Abi

To find the problem nodes:

gilgabar - August 1, 2009 - 06:46

To find the problem nodes:

  • open up /modules/node/node.module
  • find the function called _node_index_node
  • add print_r($node); to the top of that function
  • save the file
  • run cron
  • It should print a list of node ids

Edit your database with phpMyAdmin or something similar and fix or delete the problem nodes. Remove the code you added above and that should be it.

Tried that fix and got this...

ggevalt - August 2, 2009 - 16:17

... Node 0 is broken, Node 0 is broken, Node 0 is broken, Node 0 is broken, Node 0 is broken, Node 0 is broken, Node 0 is broken, Node 0 is broken,
It only ran for one line.

Any thoughts as to what this means? I do not have a node 0 in the database.

thanks

geoff

If you implemented skor's

gilgabar - August 3, 2009 - 02:28

If you implemented skor's code above in taxonomy.module you may need to disable it in order to see the results from adding print_r($node); to node.module.

I had this issue as well, and

Ryan Palmer - August 25, 2009 - 21:24

I had this issue as well, and discovered node_load() was not returning a valid node because the INNER JOIN on the users table prevents the loading of nodes without a valid user record in the users table.

It's important to set the node author uid to 0 (zero) so the node table record can be joined with the uid = 0 users record.

Run the following query to see if you have this same issue:

SELECT n.nid, n.uid AS original_n_uid, u.uid FROM node n LEFT JOIN users u ON n.uid = u.uid WHERE u.uid IS NULL;

thanks for the sql

biohabit - September 16, 2009 - 07:03

That SQL statement saved me quite a bit of time!

That SQL Was the Key for Fixing My Situation

Shai - September 30, 2009 - 12:46

Thanks Ryan for that SQL. That provided the info on 11 nodes whose node.uid was a uid that from a deleted user. I had upgraded a system from 4.5.5 to 6.14 so I'm not surprised I ran into something like this. But that SQL and this thread in general saved a load of troubleshooting time on my part.

The nodes returned page-not-found when trying to access them via UI. But simply going into the node table in the db and setting the node.uid to a uid that exists on the users table completely fixed the problem.

For others, note that I did not need to attempt any of the other fixes mentioned on this thread. Those other fixes, of course, may still apply in your situation. However, you may want to try running the SQL mentioned here first because if the problem is reference to a non-existent user, then you can fix your problem without touching your code at all.

Thanks much,

Shai

module

gilgabar - October 1, 2009 - 22:48

I put together a simple module for my own use to deal with the issue, based on Ryan's approach above, and I thought I would share. It lists all the broken nodes in a table and gives you the option to set the uid to zero or delete the nodes entirely if that's more appropriate in your situation. So if you are either unable or unwilling to edit your database directly you may find this helpful. It goes without saying, but use at your own risk, make backups, etc.

nodeuidcleanup.zip

Thanx it worked for me too !

panigrc - October 21, 2009 - 22:38

Thanx it worked for me too !

Glad it helped!

Ryan Palmer - October 22, 2009 - 17:48

Glad it helped!

Another fix with VID search

jmav - August 27, 2009 - 22:54

Problem appeared when i upgraded from 5.19 -> 6.13.

I used function:

  if (empty($node->taxonomy)) {
    $bad_nid = $node->nid;
    printf('Node %d is broken, ',$bad_nid);
    return;
  }

return "node 0", so i opened table node and searched for 0 in VID column (not NID !!!) and deleted it.
I remembered that i had problem with this node in the past, node type was from simplenews module.

REMEMBER: backup database !!!

re: Another fix with VID search

guusbosman - September 13, 2009 - 15:41

Thanks for this debugging information; it helped me find a broken node on my site.

In my case, I had an entry in table node, but no corresponding entry in table node_revisions. To found out which nid was the culprit, I slightly changed the function. I needed access to the original $node that was passed to the function since node_load was failing:

<?php
function _node_index_node($node) {
 
$x=$node;
 
$node = node_load($node->nid);
  if (empty(
$node->taxonomy)) {
   
print_r($x); // prints information on the $node that was originally passed to this function
   
$bad_nid = $node->nid;
   
printf('Node %d is broken, ',$bad_nid);
    return;
  }
...
}
?>

I always get this

r_honey - September 10, 2009 - 07:54

I always get this error:
Invalid argument supplied for foreach() in taxonomy.module on line 1214.

during cron run. I had not done anything like importing or something like that to break taxonomy.

As suggested above, I printed all node ids. To my surprise, all node ids belonged to nodes created by multichoice question module (installed with the quiz module). There was exactly 1 error for each multichoice question node.

Although I have incorporated the check for empty() to avoid this error, can somebody tell me what is the exact problem with the multichoice module causing this error??

--
I always think tomorrow will have more time than today.
And every today seems to pass-by faster than yesterday.
http://www.rahulsingla.com

I wonder despite so many

r_honey - October 1, 2009 - 10:00

I wonder despite so many replies to this thread, no one else has faced this issue as I have faced it, related to the multichoice question module associated with the quiz module.

I could not figure out why all multichoice question nodes triggered this problem on cron run!!!

--
I always think tomorrow will have more time than today.
And every today seems to pass-by faster than yesterday.
http://www.rahulsingla.com

I'm facing the same problems

fredklopper - October 14, 2009 - 07:40

I'm facing the same problems. Check this: http://drupal.org/node/536116

Update: Never mind. I did no see your post #7.

With regards,

Fred Klopper

taxonomy ... line 1214 error

Stephen Winters - October 22, 2009 - 16:14

I also get this "taxonomy.module on line 1214." error whenever I run cron. My website doesn't have the quiz module and I'm still trying to figure out the cause. I tried gilgabar's nodeuidcleanup.zip module, and it said "There are no broken nodes to fix." Please let me know if anyone finds a fix to this error. Thanks

Best Wishes,
Stephen

Thanks you very much! That

posterx - September 20, 2009 - 15:48

Thank you very much!
That helped me!

<?php
function _node_index_node($node) {
  $x=$node;
  $node = node_load($node->nid);
  if (empty($node->taxonomy)) {
    print_r($x); // prints information on the $node that was originally passed to this function
    $bad_nid = $node->nid;
    printf('Node %d is broken, ',$bad_nid);
    return;
  }
...
}

?>

subscribe

Stephen Winters - October 21, 2009 - 06:43

subscribe

 
 

Drupal is a registered trademark of Dries Buytaert.