| Project: | User Badges |
| Version: | 6.x-1.6 |
| Component: | Code |
| Category: | bug report |
| Priority: | major |
| Assigned: | NancyDru |
| Status: | active |
Issue Summary
Hello,
I've recently noticed this issue with my User Badges module. I'm using latest dev version and yesterday, new errors started to occur. I think it may be because of upgrade of Privatemsg module to dev version. I upgraded DB by running update.php but there were some table errors, so I downgraded it again. I've noticed few errors on msg page right after that. Though, I've removed it completly, but still, following errors occur on certain pages, this time in admin after adding a role to user:
key_exists() [<a href='function.key-exists'>function.key-exists</a>]: The second argument should be either an array or an object in file /data/b/l/bloosha.com/web/sites/all/modules/user_badges/user_badges.module on line 272Second error I've noticed occurs after I set count limit for user badges:
Invalid argument supplied for foreach() in file /data/b/l/bloosha.com/web/sites/all/modules/user_badges/user_badges.module on line 215I found, on these lines, there is always piece of code:
$account->badges_allMaybe it's somehow broken for me? Maybe it's because I'm inserting manually values into user_roles table? I would be really thankful for any kind of help.
My version of UB: http://drupal.org/node/336964
Hope we'll figure it out :)
Numline1
Comments
#1
I cannot predict what will happen if you downgrade. Please re-upgrade and explain what the error is that you are reporting.
#2
I just installed the 6.x-1.6 version of User_Badges, and i get the following error immediately after enabling the module:
"warning: Invalid argument supplied for foreach() in /path_to_directory/modules/user_badges/user_badges.module on line 215."
Line 215: foreach ($account->badges_all as $bid => $badge) { ....
Any suggestions?
#3
Hmm, I also enabled it on a new site today and it was okay. I'll do some more checking.
#4
NancyDru: Of course, it's not possible to catch everything after downgrade, but as I mentioned in the post above, it was completely uninstalled (via Drupal uninst) and new dev version was used after. You say, there was no error in your case, but have you tried to set # limit for badges to list? I may it for you on my beta site and I'll let you know. Hope we'll figure it out.
Now I'm using stable version of module and there are no errors for me.
#5
Testing done. No error occured right after install, though, error appeared after setting "Number of badges to display" to 10. Error:
warning: Invalid argument supplied for foreach() in D:\Program Files\Apache\htdocs\drupal-6.19\sites\all\modules\user_badges\user_badges.module on line 215.Tested on new / fresh Drupal 6.19
#6
Strange - the error came when I had "Number of badges to display" to 1.
I had to hack the module on line 215 from
<?phpforeach ($account->badges_all as $bid => $badge) {...
?>
to
<?phpif (is_array($account->badges_all)) {
foreach ($account->badges_all as $bid => $badge) {...
}
?>
Probably not the safest fix, but I couldn't bear seeing the error on EVERY admin page. Please let me know if there is a safer / 'cleaner' fix. Cheers...
#7
Same errors, subscribing
#8
subscribing
I have error:
warning: key_exists() [function.key-exists]: The second argument should be either an array or an object in /usr/local/www/data/magadmin/data/www/magnolia-tv.com/sites/all/modules/user_badges/user_badges.module on line 244.
#9
subscribing, guess ive got the same problem
warning: Invalid argument supplied for foreach() in /var/www/forum/modules/user_badges/user_badges.module on line 215.
#10
Subscribing.
6.x-1.6:
warning: Invalid argument supplied for foreach() in /. . ./sites/all/modules/user_badges/user_badges.module on line 215.
warning: key_exists() [function.key-exists]: The second argument should be either an array or an object in /. . ./sites/all/modules/user_badges/user_badges.module on line 244.
#11
Can someone with this problem do this for me, please:
At line 758 you will find:
if (isset($badges[$uid])) {return $badges[$uid];
}
Immediately following that, add:
$badges[$uid] = array();#12
NancyDru: That fixed it, thx. You should update it in dev branch.
#13
I will do so later today.
#14
I rolled it back to prior stable as it is on a production site and the rollback code works with Author Pane and I did not see the duplicate badges anymore. Hope this helps.
#15
I am happy to test this fix on the dev site but was wondering if this code will also fix the Author Pane issue? if there is code to try for both I am happy to do it, if so please post all possible code bits to test and I am happy to do several tests.
#16
The change in comment #11 above fixed the issue for me. Thanks!
#17
@venusrising: what is the author pane issue/
#18
Fix from #11 committed to 6.x-1.x-dev.
#19
In authorpane shows all badges at once even if only one is selected
#20
This worked on dev site
#21
this still does not address the authorpane issue, is there new code being used?
#22
checking back in. I used the fix in #11 on a dev site and it fixes the issue. However the authorpane module still has issues where it shows all badges, does anyone know if there is any fix for this too? may be time to use a different solution other than authorpane as User Relationships new build has issues with it too now.
#23
There is an issue on Authorpane, please use that for discussion of that issue.
#24
Automatically closed -- issue fixed for 2 weeks with no activity.
#25
Looks like this still is an issue...
I'll make a patch
#26
I also noticed some unnecessary logic right before the foreach() which pertains to the variable limit that should be removed as well..
#27
Patch!
#28
Better patch..
#29
Last fix. Was misusing the static cache which was causing some problems.
Patch attached
#30
Committed to 6.x-1.x-dev (rolls out at Noon GMT). I have still not been able to recreate the issue, so I need some testers.
#31
Automatically closed -- issue fixed for 2 weeks with no activity.
#32
I just encountered the same problem after setting the max number of badges to display. After reading this thread, and setting max back to 0, the problem went away.
This was on a new install of current release on D6. I can re-create at will just by setting a limit or removing it.
#33
I just changed from 6-1.6 to the latest dev version. I no longer get error msgs displayed when setting the limit to anything other than 0, but I see all role badges displayed in the AF author pane even when set to 1. It appears that they are not being limited by the count.
#34
After changing to the dev version I still get the following "warning: key_exists() [function.key-exists]: The first argument should be either a string or an integer in C:\xampp\htdocs\coax\modules\user_badges\user_badges.module on line 244." But I no longer get the earlier error from line 215.
I changed my badges to show to "0" and still get the error. Any help, I may have to remove the module and lose functionality rather than spend more time on something that is aesthetic or a nice to have.
Thanks.
#35
#36
Is there a way someone can have a look at http://drupal.org/node/1223968#comment-4839570
Invalid argument supplied for foreach() sites/all/modules/user_badges/user_badges.module on line 215.
Think this might be related to the this issue. We have patched the module for the user badges all issue but we see the above error in the DB logs. Someone said the patch fixed it for them but no us however.
#37
Okay, i got the 215 error fixed.
However, i still cannot find a fix for the 248 error.
warning: key_exists() [function.key-exists]: The second argument should be either an array or an object in /home/jsi/public_html/sites/all/modules/user_badges/user_badges.module on line 248.
Anyone else seeing this after a registration? Do we even know what is causing it?
#38
this is still an issue, can you please have a look at fixing this
#39
I'm very busy at the moment and also trying to get a D7 version at least half-way working as I'm learning Git.
#40
After upgrading a bunch of modules I hit this.
A added the patch on #11 and error message went away, but now I notice all my author panes (which include the badges) are missing. That could be unrelated I guess.
#41
I'm now getting even if I set the number to 0. None of the roles seem to work anymore. I'm using the latest Dev branch
<?phpWarning: key_exists() [<a href='function.key-exists'>function.key-exists</a>]: The first argument should be either a string or an integer in user_badges_user() (line 280 of /sites/all/modules/user_badges/user_badges.module).
?>
Is there any fix to this? It's cause my site to apply the wrong badges to my roles!
#42
Okay, I have a solution. I think the cause is changes to PHP5.3 no longer accessing PHP objects as it used to using key_exists. Also worth noting that key_exists itself is deprecated.
I also removed the second array key check in both loops. It's no big deal - the two called functions cater for when the user already has/hasn't got the role.
At line 268 in user_badges.module (v1.6 not -dev), replace the two foreach loops with the following:
<?php
// What are the added roles?
$added = array_diff(array_keys($new_roles), array_keys((array)$account->roles));
foreach ($added as $rid) {
// if this role has a badge.
if (array_key_exists($rid, $roles)) {
$success = user_badges_user_add_badge($account->uid, $roles[$rid]->bid, 'role');
if ($success && $message) {
drupal_set_message(t('User assigned %name badge.', array('%name' => $roles[$rid]->name)));
}
}
}
// What are the removed roles?
$removed = array_diff(array_keys((array)$account->roles), array_keys($new_roles));
foreach ($removed as $rid) {
// If this role has a badge.
if (array_key_exists($rid, $roles)) {
$success = user_badges_user_remove_badge($account->uid, $roles[$rid]->bid, 'role');
drupal_set_message(t('%name badge removed from user.', array('%name' => $roles[$rid]->name)));
}
}
?>
Not sure why the second loop doesn't consider $success or $message, but left that as is.
The drupal_set_message also now include the badge name as expected.
#43
Thanks, Jeremy. Your fix worked for me.