New interest groups can be NULL

Created on 2 May 2023, over 1 year ago
Updated 19 September 2023, about 1 year ago

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.

πŸ› Bug report
Status

Closed: duplicate

Version

2.2

Component

General

Created by

πŸ‡¬πŸ‡§United Kingdom Rob230

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Rob230
  • πŸ‡¬πŸ‡§United Kingdom Rob230
  • πŸ‡¬πŸ‡§United Kingdom Rob230

    Alternatively it should return an empty array instead of NULL.

  • πŸ‡¬πŸ‡§United Kingdom Rob230

    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.

  • πŸ‡ΊπŸ‡ΈUnited States aprice42

    @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.

  • Status changed to Needs review over 1 year ago
  • Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.4 & MySQL 5.7
    last update over 1 year ago
    Waiting for branch to pass
  • πŸ‡ΊπŸ‡ΈUnited States aprice42
  • Status changed to Closed: duplicate about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States xenophyle
Production build 0.71.5 2024