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 272

Second 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 215

I found, on these lines, there is always piece of code:

$account->badges_all

Maybe 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

I cannot predict what will happen if you downgrade. Please re-upgrade and explain what the error is that you are reporting.

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?

Hmm, I also enabled it on a new site today and it was okay. I'll do some more checking.

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.

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

Strange - the error came when I had "Number of badges to display" to 1.

I had to hack the module on line 215 from

<?php
foreach ($account->badges_all as $bid => $badge) {...
?>

to

<?php
if (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...

Same errors, subscribing

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.

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.

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.

Status:Active» Postponed (maintainer needs more info)

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();

NancyDru: That fixed it, thx. You should update it in dev branch.

Status:Postponed (maintainer needs more info)» Active

I will do so later today.

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.

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.

The change in comment #11 above fixed the issue for me. Thanks!

@venusrising: what is the author pane issue/

Status:Active» Fixed

Fix from #11 committed to 6.x-1.x-dev.

In authorpane shows all badges at once even if only one is selected

This worked on dev site

this still does not address the authorpane issue, is there new code being used?

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.

There is an issue on Authorpane, please use that for discussion of that issue.

Status:Fixed» Closed (fixed)

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

Version:6.x-1.x-dev» 6.x-1.6
Status:Closed (fixed)» Active

Looks like this still is an issue...

I'll make a patch

I also noticed some unnecessary logic right before the foreach() which pertains to the variable limit that should be removed as well..

StatusFileSize
new1.8 KB

Patch!

StatusFileSize
new2.12 KB

Better patch..

StatusFileSize
new2.16 KB

Last fix. Was misusing the static cache which was causing some problems.

Patch attached

Assigned:Unassigned» NancyDru
Status:Active» Fixed

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.

Status:Fixed» Closed (fixed)

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

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.

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.

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.

Status:Closed (fixed)» Active

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.

Priority:Normal» Major

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?

this is still an issue, can you please have a look at fixing this

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.

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.

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

<?php
Warning
: 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!

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.

Thanks, Jeremy. Your fix worked for me.

Status:Active» Closed (fixed)

Fixed in Version 7. Please upgrade to Version 7.x-3.x.

Issue summary:View changes

Copied from forum