Invalid Argument

9dots - December 31, 2008 - 15:03
Project:autocategorise
Version:6.x-1.3
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed
Description

Seem to be getting an error when multiple categories / terms can apply to a post. Only one term is associated with the content and an error message is displayed at the top of the page after submission.

Error message is as follows :

warning: Invalid argument supplied for foreach() in ...../sites/all/modules/autocategorise/auto_categorise.module on line 121.

#1

matslats - January 6, 2009 - 13:55
Status:active» fixed

#2

metals83 - January 8, 2009 - 15:13

Hi,

If the module has been updated after fixing this problem. I am still getting this error.

#3

metals83 - January 8, 2009 - 15:41
Status:fixed» active

#4

Aaron Hawkins - January 9, 2009 - 13:40

This is occurring because on line 121 the module tries to run a foreach on the array of tids in a vocab. If there aren't any tids added yet then this breaks the code. There should probably be a check on the tid array before calling the _apply_terms_to_node function.

We should change this code on line 113:

$terms=get_synonyms($vid);
  foreach ($nodes as $node){
$misc[] =_apply_terms_to_node($node, $terms, $catch_all, $multiple=0);
  }

To this:

$terms = get_synonyms($vid);
if (!empty($terms)) {
  foreach ($nodes as $node){
    $misc[] = _apply_terms_to_node($node, $terms, $catch_all, $multiple=0);
  }
}

And this code on line 70:

$terms=get_synonyms($result->vid);
$vocab= taxonomy_vocabulary_load($result->vid);
$multiple=$vocab->multiple;
$misc=_apply_terms_to_node($node, $terms, $catch_all, $multiple);

To this:
$terms = get_synonyms($result->vid);
$vocab = taxonomy_vocabulary_load($result->vid);
$multiple = $vocab->multiple;
if (!empty($terms)) {
  $misc = _apply_terms_to_node($node, $terms, $catch_all, $multiple);
}

Otherwise you will get errors when the foreach tries to run on an empty array.

#5

metals83 - January 9, 2009 - 16:01

I have a bunch of terms added under the vocabulary . But it just adds one terms (alphabetically heaviest term). If i put a check if the terms are empty i don't get the errors but no terms are added (not even one).

#6

metals83 - January 9, 2009 - 16:29

ok i figured out the problem, even if you have a term but no synonym associated with it, then the problem arises. Add synonyms to the terms and it works. Problem lies in the query under get_synonym function, "WHERE vid = %d AND s.name IS NOT NULL" where s.name are for synonyms, and not all terms have synonyms necessarily.

#7

Aaron Hawkins - January 9, 2009 - 16:57

Yes, I have a patch for that issue that I posted at this page http://drupal.org/node/356506 and it points to the same solution you mentioned. We should probably keep these issues separate though so that it doesn't get marked as a duplicate.

#8

metals83 - January 10, 2009 - 08:40
Status:active» fixed

Hi,

For the benefit of those who may come across this problem.

You get this error under these two cases:

1) If no terms are defined
2) If no synonyms are defined

Both of these result in an empty array hence the error.

To solve this problem put a check for the empty array as mentioned in comment #4 by Aaron and then apply the patch posted here http://drupal.org/node/356506 (posted by Aaron).

OR

if you don't want to apply these patch and want to wait till the next official release of the module with these bugs fixed, define a bunch of terms and assign a synonym to all the defined terms (you can find the option for declaring a synonym while you add/edit a term under Advanced section, it remains collapsed by default)

With this i take the liberty to mark this thread closed.

Thanks Aaron.

--Cheers

#9

System Message - January 24, 2009 - 08:50
Status:fixed» closed

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

#10

kiamlaluno - January 24, 2009 - 09:08
Status:closed» active

A bug report should be set as fixed if the maintainer has committed the fixed code in CVS, not when somebody fixes his own copy of the module.

#11

skizzo - May 7, 2009 - 15:29
Version:6.x-1.2» 6.x-1.3

I am still seeing the OP error message in 6.x-1.3 when adding a node.

#12

matslats - May 13, 2009 - 11:37
Status:active» fixed

#13

System Message - May 27, 2009 - 11:40
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.