A couple places in the module the $vids are loaded as such:

$vids = variable_get('user_terms_vocabs', '');

And then foreach-ed over. But when it returns just a single value it's not an array and then gives errors.

I've patched for the client using this:

if(!is_array($vids)){
$vids = array($vids);
}

And it seems to be fine.

Comments

joachim’s picture

Status: Active » Postponed (maintainer needs more info)

When do you get a single value out of it?

I tried going to the admin settings and enabling just one vocabulary, and that variable is still an array, just with only one entry.

kanani’s picture

Experiencing this behavior as well.

Invalid argument supplied for foreach() in /Users/davidhazel/workspace/pau_git/sites/all/modules/user_terms/user_terms.module on line 181.

If only one vocabulary is selected, $vids is a string when it gets passed to user_terms_view_profile_separated.

One work around is to cast it as an array before running the foreach.

$vids = (array) $vids;

/**
* Show terms on the user's profile by vocabulary.
*/
function user_terms_view_profile_separated(&$account, $vids) {
$terms = $account->user_terms;
$vids = (array) $vids;
foreach ($vids as $vid) {

$term_names = array();

foreach ($terms as $tid => $term) {
if ($term['vid'] == $vid) {
$term_names[] = check_plain($terms[$tid]['name']);
unset($terms[$tid]);
}
}

if (!empty($term_names)) {
$vocab = taxonomy_vocabulary_load($vid);
user_terms_view_profile_item($account, 'user_terms_' . $vid, $vocab->name, $term_names, $vocab->weight);
}
}
}

Another alternative is that in user_terms.admin.inc on line 28

$form['user_terms_vocabs'] = array(
'#type' => 'select',
'#title' => t('User vocabularies'),
'#options' => $options,
'#description' => t('Choose the vocabularies that will be used to tag users. To select multiple values, hold down the Control key while clicking. If you are using an Apple, hold down the Apple or Command key.'),
'#default_value' => variable_get('user_terms_vocabs', ''),
'#multiple' => TRUE,
);

to make sure that it gets saved as an array.

joachim’s picture

I'm really not able to reproduce this at all, so I'm going to need more information.

Are you using any other modules that might change the user form?

kanani’s picture

I think it might have to do with a legacy user_terms_vocabs value stored in {variable}.

I did a fresh drupal 6.17 install with just user_terms installed and verified that an array was indeed being returned if only one vocab was selected.

I then completely uninstalled/reinstalled user_terms from my client site, which has quite a few modules installed, and an array was being returned.

I then reverted to a db snapshot taken prior to the uninstall/reinstall and the error came back up again.

To fix, without uninstall/reinstall I just ran a query against the db

update variable set value = 'a:1:{i:1;s:1:"1";}' where name = 'user_terms_vocabs';

and updated the value stored in the {variable} table.

In my case the vocab id was 1 so I replaced
s:1:"1";
with
a:1:{i:1;s:1:"1";}

Problem solved.

joachim’s picture

Status: Postponed (maintainer needs more info) » Active

Wow that's some good detective work!

I've gone for a look in CVS for that variable in older versions, and this is how it got a bad value:

- version 1.1 of the user_terms.module file (corresponding to beta 1 of this whole module) had only a single vocabulary, and stored a flat number in the variable user_terms_vocabulary.
- the next version of the module file switches to allow multiple vocabs, stored in user_terms_vocabs.
- commit 1.1.2.1 of the .install file does a conversion for users coming from beta 1:

 variable_set('user_terms_vocabs', variable_get('user_terms_vocabulary', ''));

This is the culprit!

The new variable should be an array, and yet it is getting set to a flat value.

I'll write a patch :)

joachim’s picture

Status: Active » Fixed
StatusFileSize
new1.25 KB

Confirming I can reproduce this bug if I install beta 1 then upgrade.

Here's a patch.

I've tested it by installing beta 1 of this module, and upgrading to the fixed code.

If you're getting this bug, the patch won't help you as you've already run the update function on your database. The simple fix however is to visit your user terms setting page and save the configuration -- that will overwrite the variables with the proper values.

Fixed. Thanks kanani for the debuging work!

Status: Fixed » Closed (fixed)

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