After upgrading the MailChimp module from 7.x-3.x to 7.x-4.x (and the API library), this PHP warning appears after creating or updating a user account:

Warning: Invalid argument supplied for foreach() in mailchimp_lists_process_subscribe_form_choices() (line 264 of /srv/bindings/fb51db2c21174e7bacb47f0744f58f26/code/sites/all/modules/mailchimp/modules/mailchimp_lists/mailchimp_lists.module).

The code in question is this:

    $interest_groups = (isset($choices['interest_groups'])) ? $choices['interest_groups'] : array();
    dpm($interest_groups);   // I added this
    // Parse interests from interest groups.
    $interests = array();
    foreach ($interest_groups as $interest_group) {
      foreach ($interest_group as $interest_id => $interest_status) {   // this is line 264
        $interests[$interest_id] = ($interest_status !== 0);
      }
    }

On my site the subscription field uses interest groups, and the user may only select one of them (they're radio buttons).

dpm() reports this:

... (Array, 1 element)
    dba908573b (String, 10 characters ) a0ebfb5959

I don't know this module well enough to know if line 264 is supposed to always expect an array instead of a string or if this is oversight, but in the case of my site line 264 is trying to process a string as an array.

I added debug output to mailchimp_update_member_process() in mailchimp.module for $merge_vars and $interests, and $merge_vars has all the data it's supposed to have, but $interests is an empty array.

Meanwhile in the system log every instance of the error message above is followed by this:

[redacted email address] was updated in list caaa257849_id.

That message is sort-of accurate and sort-of inaccurate because the $merge_vars stuff still works but $interests does not.

I tried the latest development release and the error message still happens.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Lord Pachelbel created an issue. See original summary.

Lord Pachelbel’s picture

Issue summary: View changes
Lord Pachelbel’s picture

For now I'm patching it with this:

// Parse interests from interest groups.
$interests = array();
foreach ($interest_groups as $interest_group) {
  $interests[$interest_group] = TRUE;
}

I'm not submitting a patch file because I suspect any fix for https://www.drupal.org/node/2530626 will affect the code above because they're closely related issues.

From my tracing through the code it seems to me that the main problem is inconsistent handling of ['#default_value'] pertaining to interest groups. In some cases ['#default_value'] stores an interest group ID (a string), in some cases it stores an array of interest group IDs, and in mailchimp_interest_groups_form_elements() it stores a two-dimensional array (an array of arrays of interest group IDs).

I spent a few hours today trying to straighten it all out, but it's one of those problems in which every little fix I try breaks something else because of the inconsistencies with ['#default_value'].

johnpicozzi’s picture

@Lord Pachelbel - I tried the code above and got the following offset error.

Illegal offset type in mailchimp_lists_process_subscribe_form_choices() (line 264 of /Users/johnpicozzi/Sites/leica/docroot/sites/all/modules/contrib/mailchimp/modules/mailchimp_lists/mailchimp_lists.module).

Lord Pachelbel’s picture

I'm using 7.x-4.7+1-dev if that helps.

nlarow’s picture

@Lord Pachelbel I can't seem to get the older +1 dev version off of the repository or drush, it's up to +3. Any advice on getting the older version? I wanted to test your patch with that +1 branch, still trying to beat this problem.. I sent you an email yesterday with my contact info if needed. Thanks!

Lord Pachelbel’s picture

FileSize
2.05 MB

This zip file contains the module and library I'm using.

nlarow’s picture

Thank you! Appreciated

lukedekker’s picture

I'm getting the same issue, but in the MailChimp module itself. Warning: Invalid argument supplied for foreach() in mailchimp_update_local_cache() (line 607 ...

Started happening after upgrade from 3.x.

foreach ($args['interests'] as $interest_group => $interests) {
  foreach ($interests as $interest_id => $value) { // This is the line in question
    if ($value !== 0) {
      $cache->interests->$interest_id = TRUE;
    }
  }
}
nlarow’s picture

@Lord Pachabel, thank you but no luck.. Still the same error signing up new subscribers that are not on the list already.. Patching existing subscribers works ok, unsub/deletion seems to be working.. just new subscribers seem to fail on the "Interest Areas" ~

Same results with yesterday's dev release.. seems this is still an ongoing issue :( MailChimp's API log:

GuzzleHttp/6.2.1 curl/7.19.7 PHP/5.5.38 3.0 The requested resource could not be found.
GuzzleHttp/6.2.1 curl/7.19.7 PHP/5.5.38 3.0 Your merge fields were invalid.

Even though the Drupal error log refers to an invalid ADDRESS merge field, it's actually the "Interest Areas" string/array that craps out the new subscriber process. The error reporting is incorrect, patching existing subscribers will merge all those fields properly, but will not deal with the Interest Areas correctly. And no it's not a blocked/testing account:

An error occurred subscribing redacted@email.com to list XXXXXXXXXX. Status code 400. "400: Invalid Resource - Your merge fields were invalid. a:1:{i:0;O:8:"stdClass":2:{s:5:"field";s:7:"ADDRESS";s:7:"message";s:31:"Please enter a complete address";}}"

nlarow’s picture

FYI, I just rolled back to 7.x-3.6 today to alleviate the issue until it's corrected in the 4.x. A simple module/library replace + flush cache and it's signing up new members fine now in depreciated v2.0 mode.. should hold us over until they make more progress with this module. If anyone needs the old library (it's not on the repository anymore) for 7.x-3.6 just let me know.

Lord Pachelbel’s picture

I think I have it working (using 7.x-4.8+1-dev). This patch relies on the interest group caching patch at https://www.drupal.org/node/2835243#comment-11824409

Other patches you will probably want to use:
https://www.drupal.org/node/2831012
https://www.drupal.org/node/2530626#comment-11980729

lukedekker’s picture

lukedekker’s picture

rjacobsen0 credited drumm.

rjacobsen0’s picture

Crediting from a duplicate issue.

  • rjacobsen0 committed 9111b8a on 7.x-5.x authored by lukedekker
    Issue #2838871 by lukedekker, Lord Pachelbel, nlarow, drumm, johnpicozzi...

  • rjacobsen0 committed 72d643c on 7.x-4.x authored by lukedekker
    Issue #2838871 by lukedekker, Lord Pachelbel, nlarow, drumm, johnpicozzi...
rjacobsen0’s picture

Version: 7.x-4.x-dev » 7.x-5.x-dev
Status: Needs review » Fixed

Committing. You can see more discussion on this duplicate issue https://www.drupal.org/project/mailchimp/issues/2857646#comment-12902305. Thank you everyone!

Status: Fixed » Closed (fixed)

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

entrepreneur27’s picture

Version: 7.x-5.x-dev » 7.x-5.4

Hi: I have Mailchimp 7.x-5.4 and seem to still have this issue so I am reopening it.

To be specific: I have several interest groups, and one of them has radio buttons. When I try and change the radio button interest group I get the error message below, and the interest group I elected is not saved.

Any ideas?

Warning: Invalid argument supplied for foreach() in _mailchimp_lists_field_postsave() (line 698 of /srv/bindings/fbca292e623a4b9caf138d968951b5b3/code/sites/all/modules/contrib/mailchimp/modules/mailchimp_lists/includes/mailchimp_lists.field.inc).
Warning: Invalid argument supplied for foreach() in mailchimp_lists_process_subscribe_form_choices() (line 265 of /srv/bindings/fbca292e623a4b9caf138d968951b5b3/code/sites/all/modules/contrib/mailchimp/modules/mailchimp_lists/mailchimp_lists.module).

drumm’s picture

This fix does not look quite correct.

    foreach ($args['interests'] as $interest_group => $interests) {
      if(is_array($interest_group)){

As an array key, $interest_group will never be an array, so that if statement will never be true.

drumm’s picture

Version: 7.x-5.4 » 7.x-5.x-dev

I re-opened #2857646: Warning: Invalid argument supplied for foreach() in mailchimp_update_local_cache(), since the patch there has a better if condition.