We are seeing this error occurring very regularly in our logs:

Warning: Invalid argument supplied for foreach() in _mailchimp_lists_subscription_has_changed() (line 229 of /var/www/prod/csp/site/modules/contrib/mailchimp/modules/mailchimp_lists/mailchimp_lists.module)

However, I've spent a long time trying to debug it or reproduce it and could not. I suspect it could be something to do with people who haven't visited the site for a while and are lacking data (from a newer version of the module), or some configuration problem, but honestly I don't know why it could be NULL.

But regardless of why it happens, logically speaking it can be easily prevented with a simple change to add ?? [] before the foreach.

Logically this makes sense because the result of getInterestGroups() can be NULL:

public function getInterestGroups() {
  if (isset($this->values['interest_groups'])) {
    return $this->values['interest_groups'];
  }

  return NULL;
}

So this should be handled in the code.

Comments

Rob230 created an issue. See original summary.

rob230’s picture

rob230’s picture

Alternatively it should return an empty array instead of NULL.

rob230’s picture

StatusFileSize
new792 bytes

That actually still didn't fix the error, because it turns out it can be FALSE as well. At least that is what is on the entity, I assume it is the same after an entity is saved.

$user->get('field_mailchimp_field')->get(0);
=> Drupal\mailchimp_lists\Plugin\Field\FieldType\MailchimpListsSubscription {#8216
   value: [
     "subscribe" => "0",
     "interest_groups" => false,
   ],
 }

Notice "subscribe" is also "0" rather than FALSE, so maybe this is data from an earlier version of the module which is causing it to have errors. I don't know if there should be something done to fix the data, e.g. to make it like this:

[
  "subscribe" => false,
  "interest_groups" => [],
]

But here is a patch to just fix the PHP error.

aprice42’s picture

StatusFileSize
new1.74 KB

@Rob230 I wonder if this patch might resolve the issue? Just adding a check over that entire block of code to see if the new_interest_groups exists and is an array.

aprice42’s picture

Status: Active » Needs review
xenophyle’s picture

Status: Needs review » Closed (duplicate)
Related issues: +#3194699: MailchimpListsSubscription::getInterestGroups() should always return an array