diff --git a/campaignmonitor.module b/campaignmonitor.module index 580d8e2bdfcb10b258459d8f03f7c5761a0adf65..6d4e427c962b731d04f471fd32e355b21848d8cf 100644 --- a/campaignmonitor.module +++ b/campaignmonitor.module @@ -8,7 +8,6 @@ */ use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\Core\Entity\EntityInterface; /** * Implements hook_help(). @@ -45,17 +44,3 @@ function campaignmonitor_theme($existing, $type, $theme, $path) { ], ]; } - -/** - * Implements hook_ENTITY_TYPE_delete(). - */ -function campaignmonitor_user_delete(EntityInterface $entity) { - // Unsubscribe users when deleted. - $mail = $entity->get('mail')->getValue()[0]['value']; - $campaignManager = \Drupal::service('campaignmonitor.manager'); - $campaignSubscriptionManager = \Drupal::service('campaignmonitor.subscription_manager'); - $lists = $campaignManager->getLists(); - foreach ($lists as $list_id => $values) { - $campaignSubscriptionManager->userUnsubscribe($list_id, $mail); - } -} diff --git a/modules/campaignmonitor_user/campaignmonitor_user.module b/modules/campaignmonitor_user/campaignmonitor_user.module new file mode 100644 index 0000000000000000000000000000000000000000..6a105cff12ec409ea29be6dadd676c2d97eac1aa --- /dev/null +++ b/modules/campaignmonitor_user/campaignmonitor_user.module @@ -0,0 +1,26 @@ +<?php + +/** + * @file + * Adds support for Campaign Monitor in the user profile. + */ + +use Drupal\Core\Entity\EntityInterface; + +/** + * Implements hook_ENTITY_TYPE_delete(). + */ +function campaignmonitor_user_user_delete(EntityInterface $entity) { + // Determine of the users need to be unsubscribed on deletion. + $config = \Drupal::config('campaignmonitor.settings'); + $account_delete = $config->get('account_delete_option'); + if ($account_delete) { + $mail = $entity->get('mail')->getValue()[0]['value']; + $campaignManager = \Drupal::service('campaignmonitor.manager'); + $campaignSubscriptionManager = \Drupal::service('campaignmonitor.subscription_manager'); + $lists = $campaignManager->getLists(); + foreach ($lists as $list_id => $values) { + $campaignSubscriptionManager->userUnsubscribe($list_id, $mail); + } + } +} diff --git a/src/Form/CampaignMonitorAdminForm.php b/src/Form/CampaignMonitorAdminForm.php index 301d7faeb81e8672e36cc5088b3147213b481efd..fc0fdb306ef42ddff04a629570251e40b930abd4 100644 --- a/src/Form/CampaignMonitorAdminForm.php +++ b/src/Form/CampaignMonitorAdminForm.php @@ -158,6 +158,13 @@ class CampaignMonitorAdminForm extends ConfigFormBase { ]; } + $form['account_delete_option'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Unsubscribe users from campaigns on account deletion.'), + '#description' => $this->t('Should the users be unsubscribed from all the campaigns if their accounts were deleted?'), + '#default_value' => $config->get('account_delete_option'), + ]; + $form['cron'] = [ '#type' => 'checkbox', '#title' => 'Use batch processing.', @@ -246,6 +253,7 @@ class CampaignMonitorAdminForm extends ConfigFormBase { 'campaignmonitor_general', 'subscription_confirmation_text', ])) + ->set('account_delete_option', $form_state->getValue('account_delete_option')) ->set('cron', $form_state->getValue('cron')) ->set('batch_limit', $form_state->getValue('batch_limit')) ->save();