warning: Invalid argument supplied for foreach() in user.module on line 1507

Created on 25 October 2007, over 16 years ago
Updated 3 June 2024, 29 days ago

Hi, I've come across an error when I have anonymous users unable to flag content and they are viewing a users profile.

I tracked it down to...

function flag_content_user($op, &$edit, &$account, $category = NULL) {
  global $user;

  switch($op) {
    case 'view':
      if ($user->uid == $account->uid) {
        // User is viewing their own user page, don't show them anything
        return array();
      }

      $links = flag_content_link(FLAG_CONTENT_TYPE_USER, $account);
      foreach($links as $key => $value) {
        $items[] = array(
          'class' => $key,
          'value' => l($value['title'], $value['href'], $value['attributes']),
        );
      }
      return array(t('Flag user') => $items);

    case 'delete':
      db_query("DELETE FROM {flag_content} WHERE eid = %d AND type = '%s'", $account->uid, FLAG_CONTENT_TYPE_USER);
      break;
   }
}

It looks like if flag_content_link() returns an empty array the foreach loop in flag_content_user() never loops which results in $items array not being created. This then messes up user.module when we... return array(t('Flag user') => $items);
I was able to bandaid the problem by adding...

$items = array();

Here is what the function looks like for me now.

function flag_content_user($op, &$edit, &$account, $category = NULL) {
  global $user;

  switch($op) {
    case 'view':
      if ($user->uid == $account->uid) {
        // User is viewing their own user page, don't show them anything
        return array();
      }
      
      $items = array();
      $links = flag_content_link(FLAG_CONTENT_TYPE_USER, $account);
      foreach($links as $key => $value) {
        $items[] = array(
          'class' => $key,
          'value' => l($value['title'], $value['href'], $value['attributes']),
        );
      }
      return array(t('Flag user') => $items);

    case 'delete':
      db_query("DELETE FROM {flag_content} WHERE eid = %d AND type = '%s'", $account->uid, FLAG_CONTENT_TYPE_USER);
      break;
   }
}
πŸ› Bug report
Status

Fixed

Version

2.5

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.69.0 2024