Have spent several hours looking into a bug when using content_profile_registration with the Mailchimp module. When content profile fields (via tokens) are used as merge variables in the Mailchimp settings the profile fields are not being returned correctly at all.

After extensive debugging I've tracked it down to two things:

Firstly, the mailchimp module uses hook_user('insert'...). If I've understood correctly the content profile node hasn't been created at that point, since it uses a custom submit handler on the register form instead.

I managed to hack my way around a) by disabling the normal mailchimp setup and hooking into it with my own module's submit handler which runs after Mailchimp. However, it still wasn't working, and this was due to the caching in content_profile_load. Tokens get requested before the content profile is created, and then get requested again afterwards - but content_profile_load has already cached the fact the content doesn't exist, so never gets looked up again. I again managed to get around this with a custom module by calling content_profile_load with $reset=TRUE at the right point in the process.

While I now have things working for me, any thoughts on the best way to get these two modules working together in general? I'd imagine allowing the profile to be accessed via the hook_user('insert') method would be handy for other modules.

Comments

joeysantiago’s picture

Well...

here are my two cents. I used another approach: copied some code from mailchimp in order to repeat some mailchimp operations on hook_nodeapi. This is the function i used (since i had to use some tokens coming from select cck fields and there's an issue in content profile token not showing those, i had to hardcode those fields)

/*
 * Implementing hook_nodeapi
 */
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
  if($node->type=="profile"){
    if(($op=="presave") && variable_get('mailchimp_user_register', FALSE)&& $q = _mailchimp_get_api_object()) {
        $edit['mailchimp_lists'] = _mailchimp_get_required_lists();
        foreach ($edit['mailchimp_lists'] as $list) {
            // subscribe the user if they are not previously subscribed or update existing subscriptions
            $merge_vars = _mailchimp_load_user_list_mergevars($node->uid, $list->id, $q->listMergeVars($list->id));
            foreach($list->mergevars as $key=>$var){
                if(isset($var)){
                    $var = str_replace("token_content-profile-profile-","",$var);
                    $var = str_replace("-raw","",$var);
                    $field = "field_".$var;
                    $var = $node->$field;
                    $var = $var[0]['value'];
                    $mymerge_vars[$key] = $var;
                }
            }
            $mymerge_vars['MMERGE5'] = $node->field_nazione[0]['value'];
            $mymerge_vars['MMERGE11'] = "SOMEVALUE"; //used to understand the user comes from here
            $mymerge_vars['MMERGE12'] = $node->field_professione[0]['value'];
            $user = user_load($node->uid);
            $ret = _mailchimp_subscribe_user($list, $user->mail, $mymerge_vars, TRUE, $q);
            if (!$ret) {
                watchdog('mailchimp', $q->errorMessage, NULL, WATCHDOG_ERROR);
            }
            else{
                watchdog('mailchimp', 'MailChimp from mymodule: %email updated in list %list on action %action',
                    array('%email' => $user->mail, '%list' => $list->name, '%action' => $op), WATCHDOG_NOTICE);
            }
        }
      }

    }
}