You are here

function mailchimp_lists_entity_delete in Mailchimp 8

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_entity_delete()
  2. 7.3 modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_entity_delete()
  3. 7.4 modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_entity_delete()
  4. 2.x modules/mailchimp_lists/mailchimp_lists.module \mailchimp_lists_entity_delete()

Implements hook_entity_delete().

File

modules/mailchimp_lists/mailchimp_lists.module, line 23
Mailchimp lists/audiences module.

Code

function mailchimp_lists_entity_delete($entity) {

  // Only act on content entities.
  if (!$entity instanceof ContentEntityInterface) {
    return;
  }
  $field_definitions = $entity
    ->getFieldDefinitions();
  if (empty($field_definitions)) {
    return;
  }

  // Filter fields to only subscription fields marked to unsubscribe on delete.
  $list_fields = array_keys(array_filter($field_definitions, function (FieldDefinitionInterface $field) {
    return $field
      ->getType() == 'mailchimp_lists_subscription' && $field
      ->getSetting('unsubscribe_on_delete');
  }));

  /* @var $item \Drupal\mailchimp_lists\Plugin\Field\FieldType\MailchimpListsSubscription */
  foreach ($list_fields as $field) {

    // Additional foreach to support multiple values.
    foreach ($entity
      ->get($field) as $item) {
      mailchimp_lists_process_subscribe_form_choices([
        'subscribe' => FALSE,
      ], $item, $entity);
    }
  }
}