When the user object has complex data added by other modules (for example $user->my_mod_data = stdClass(....)), the MailChimp module generates a warnings when subscribing a user:

Warning Object of class stdClass could not be converted to string in sites/all/modules/mailchimp/mailchimp.module ...

After looking through the code, it seems that mailchimp is processing all fields, which some exception cases for handling the standard objects. I briefly thought about creating yet another hook to allow my module to tell mailchimp not to process it's user fields, but then decided it was probably reasonable for mailchimp to process all modules, and just not display warnings when something failed (log them instead.)

I've attached a patch that does that.

Comments

levelos’s picture

I would rather just test for a string value in the else condition. No?

asacamano’s picture

So to get down to the nitty gritty, you're proposing changing

elseif ($key != 'roles') {
$out[$key] = $value;
}

to

elseif ($key != 'roles' && is_scalar($value)) {
$out[$key] = $value;
}

That works in some cases. However, I've gone and put an array of objects in my user, so the revised function would look like this (which works):

function mailchimp_mailchimp_merge_values($user) {
$out = array();
$out = (array)$user;
foreach ((array)$user as $key => $value) {
if (is_array($value) && $key != 'role') {
$dup = $value;
foreach ($dup as $key2=>$value2) {
if (!is_scalar($value2)) {
unset($dup,$key2);
}
}
if (is_array(dup) && count($dup) > 0) {
$out[$key] = implode('/', $dup);
} else {
$out[$key] = '';
}
}
elseif (in_array($key, array('login', 'access', 'created'))) {
$out[$key] = date('c', $value);
}
elseif ($key != 'roles' && is_scalar($value)) {
$out[$key] = $value;
}
}

$out = array_merge($out, _mailchimp_get_user_tokens($user));
return $out;
}

It is better than my patch in that it actually works instead of ignoring errors. What do you think about this one?

elliotttf’s picture

StatusFileSize
new1.5 KB

I can confirm asacamano's solution works and have rolled a patch. Also, while testing this I found that users could not subscribe to lists from their profile page because the $form_state['values']['mail'] variable was not set so I have included a fallback for that case in the same patch.

levelos’s picture

Status: Active » Closed (fixed)