Problem/Motivation
I wanted to check a MailchimpListsSubscription Entity for the 'subscribed' value through using
$subscription->getSubscribe()
Strange enough it always returned NULL. Although the checkbox seemed to save the changes when I checked/unchecked it.
The problem was that, during building the form/widget, the code checked the DB first and immediatly after checks mailchimp for the subscription and sets the default value for the field.
mailchimp_d8/modules/mailchimp_lists/src/Plugin/Field/FieldWidget/MailchimpListsSelectWidget.php::formElement():33
// Check the current Instance / read from DB
$subscribe_default = $instance->getSubscribe();
$email = NULL;
if (!empty($instance->getEntity())) {
$email = mailchimp_lists_load_email($instance, $instance->getEntity(), FALSE);
if ($email) {
// Check mailchimp itself
$subscribe_default = mailchimp_is_subscribed($instance->getFieldDefinition()->getSetting('mc_list_id'), $email);
}
}
It is possible that there are 2 different states on Mailchimp and in the DB which should be checked for. But thats another problem.
The actual problem that getSubscribe() doesn't work is because of the extra ['value'] array dimension in several places, so the field in the DB never gets actually updated.
Proposed resolution
Using the ['values'] property of the Instance of the Class correctly.
Removing the extra ['value'] dimension where necessary.
Remaining tasks
I havent tested this thoroughly, but it doesn't seem like a big change.
Also, as seen in the patch, the property value ['interest_groups'] was affected. It threw an error at me while viewing the field settings of a 'Mailchimp Subscription' field. Not anymore.
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | mailchimp-save-subscribe-to-db-2903853-7.patch | 1.95 KB | khiminrm |
| #2 | 2903853-2.patch | 1.93 KB | vierlex |
Comments
Comment #2
vierlexComment #3
vierlexComment #4
vierlexComment #5
grienauerCould a maintainer take a quick look at this issue please?
We use this patch at one site and it now works.
thx!
Comment #6
grienauerComment #7
khiminrm commentedI've updated patch for last stable version of Mailchimp (8.x-1.8).
Could it be committed, please?
Comment #8
khiminrm commentedComment #9
init90We use the patch of one of our project for a long time and all works good.
Comment #10
sivaprasadc commented@vierlex
Thanks for the patch, It works like a charm.
Comment #11
stephen ollmanPatched tested and works as advertised.
Many thanks!
When will this patch be rolled into the latest release?
Comment #12
rviner commentedFollowing on from this I also noticed you can no longer unsubscribe from a list. The allow_unsubscribe parameter is always False despite this being true in the settings.
Comment #13
rviner commentedCreated a new issue for this: Can't unsubscribe from lists
Comment #14
samuel.mortensonComment #15
jtotheeannie commentedWe've applied the patch on our site and its working wonderfully when extracting the subscription status from the database.
However when using a webhooks, the user unsubscribes through the mailchimp interface, the checkbox is unchecked on the Drupal side, but the database is then not updated to reflect that the user is unsubscribed.
If we re-save the user info then the database is updated, but it would be nice if it was updated automatically as opposed to re-checking the mailchimp cleaned list and re-saving all users individually.
Comment #16
mshaver commentedComment #18
samuel.mortensonThanks for the fix all. @Jtotheeannie Could you open a follow up issue for that problem with replication steps? Thank you!
Comment #19
jtotheeannie commentedCreated new issue Unsubscribing on Mailchimp through webhook doesnt update DB