-
Notifications
You must be signed in to change notification settings - Fork 5
Add Opt Out field and MailChimp integration #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JeremySkinner
wants to merge
8
commits into
FreelyGive:proto/nw-demo
Choose a base branch
from
JeremySkinner:feature/communications-and-mailing
base: proto/nw-demo
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eb22915
Add Communications profile type, opt-out field and mailchimp integrat…
JeremySkinner be52a8f
Set opt out when user unsubscribes from mailchimp
JeremySkinner a0b8cda
Apply patch for broken hook_entity_field_storage_info
JeremySkinner c36bdd1
Create the opt-out field through code instead of config and ensure th…
JeremySkinner b967ccb
Reformatting and code standards fixes
JeremySkinner cb78fea
Fix view display type for opt out field
JeremySkinner aff0cc4
Coder review; Line length
JeremySkinner 7a54ce7
Update tab preferences based on code review
JeremySkinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
config/install/contacts.contact_tab.crm_communications_tab.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| langcode: en | ||
| status: true | ||
| dependencies: | ||
| config: | ||
| - profile.type.crm_communications | ||
| module: | ||
| - profile | ||
| id: crm_communications_tab | ||
| label: Communications | ||
| path: communications | ||
| layout: contacts_tab_content.stacked | ||
| weight: null | ||
| relationships: | ||
| profile_crm_communications: | ||
| id: 'typed_data_entity_relationship:entity:user:profile_crm_communications' | ||
| name: profile_crm_communications | ||
| source: user | ||
| blocks: | ||
| contacts_entity_profile_crm_communications: | ||
| id: 'contacts_entity:profile-crm_communications' | ||
| label: 'Communication Preferences' | ||
| provider: contacts | ||
| label_display: '0' | ||
| mode: view | ||
| create: true | ||
| operation: contacts_dashboard | ||
| view_mode: contacts_dashboard | ||
| edit_link: content | ||
| edit_id: edit | ||
| region: top | ||
| context_mapping: | ||
| entity: profile_crm_communications | ||
| user: user | ||
| name: contacts_entity_profile_crm_communications |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| langcode: en | ||
| status: true | ||
| dependencies: { } | ||
| id: crm_communications | ||
| label: Communications | ||
| registration: false | ||
| multiple: false | ||
| roles: | ||
| authenticated: '0' | ||
| administrator: '0' | ||
| crm_indiv: '0' | ||
| crm_org: '0' | ||
| student: '0' | ||
| weight: 0 | ||
| use_revisions: false | ||
| description: '' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| name: Contacts MailChimp Integration | ||
| description: "MailChimp integration for the Contacts module" | ||
| type: module | ||
| core: 8.x | ||
|
|
||
| dependencies: | ||
| - contacts | ||
| - mailchimp_lists (>=8.x-1.6) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @file | ||
| * Module related hook implementations for the contacts_mailchimp module. | ||
| */ | ||
|
|
||
| use Drupal\profile\Entity\Profile; | ||
| use Drupal\Core\Entity\EntityInterface; | ||
|
|
||
| /** | ||
| * Implements hook_entity_presave(). | ||
| */ | ||
| function contacts_mailchimp_entity_presave(EntityInterface $entity) { | ||
| // Unsubscribe any mailchimp mailinglists if the user has opted out of comms. | ||
| $field_name = 'crm_opt_out'; | ||
|
|
||
| if ($entity->getEntityTypeId() == 'profile' && $entity->bundle() == 'crm_communications' && $entity->hasField($field_name)) { | ||
| $opt_out = $entity->get($field_name)->value; | ||
|
|
||
| if (isset($entity->original)) { | ||
| $original = $entity->original->get($field_name)->value; | ||
| } | ||
| else { | ||
| $original = NULL; | ||
| } | ||
|
|
||
| // TODO: Investigate making this asynchronous. | ||
| if (($original === 0 || $original == NULL) && $opt_out === 1 && $entity->uid->entity->hasField('mail')) { | ||
| $email = $entity->uid->entity->mail->value; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not many ways of doing asynchronous in Drupal. We could consider moving it to a cron queue for performance, but its probably fine for now. |
||
| $lists = mailchimp_get_lists_for_email($email); | ||
| foreach ($lists as $list) { | ||
| mailchimp_unsubscribe($list->id, $email); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Implements hook_mailchimp_process_webhook(). | ||
| */ | ||
| function contacts_mailchimp_mailchimp_process_webhook($type, $data) { | ||
| // If user unsubscribes from mailchimp, treat it as a full opt-out. | ||
| if ($type == 'unsubscribe') { | ||
| $ids = \Drupal::entityQuery('profile') | ||
| ->condition('uid.entity.mail', $data['email']) | ||
| ->condition('type', 'crm_communications') | ||
| ->execute(); | ||
|
|
||
| $profiles = Profile::loadMultiple($ids); | ||
|
|
||
| /* @var $profile \Drupal\profile\Entity\Profile */ | ||
| foreach ($profiles as $profile) { | ||
| if ($profile->hasField('crm_opt_out')) { | ||
| $profile->set('crm_opt_out', 1); | ||
| $profile->save(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there not a mailchimp patch as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yanniboi Yes - this is only needed if you install contacts_mailchimp though, so should it go in the composer.json for the contacts module, or would there need to be a new composer.json specifically for contacts_mailchimp?