You are here

function _mailchimp_lists_field_postsave in Mailchimp 7.4

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_lists/includes/mailchimp_lists.field.inc \_mailchimp_lists_field_postsave()
  2. 7.3 modules/mailchimp_lists/includes/mailchimp_lists.field.inc \_mailchimp_lists_field_postsave()

If we have any mailchimp_lists_subscription fields, we handle any changes to them by making appropriate subscription calls.

2 calls to _mailchimp_lists_field_postsave()
mailchimp_lists_field_insert in modules/mailchimp_lists/includes/mailchimp_lists.field.inc
Implements hook_field_insert().
mailchimp_lists_field_update in modules/mailchimp_lists/includes/mailchimp_lists.field.inc
Implements hook_field_update().

File

modules/mailchimp_lists/includes/mailchimp_lists.field.inc, line 665
Field hooks.

Code

function _mailchimp_lists_field_postsave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if ($field['type'] == 'mailchimp_lists_subscription') {
    $entity_wrapper = entity_metadata_wrapper($entity_type, $entity);
    $choices = $entity_wrapper->{$instance['field_name']}
      ->value();
    if (!isset($entity->is_new)) {
      $entity->is_new = $entity_wrapper
        ->getIdentifier() === FALSE || $entity_wrapper
        ->getIdentifier() === NULL;
    }

    // Check if the field is empty or is set by the system via field_get_default_value().
    if (empty($items[0])) {

      // user_save() was called without our field in its $edit
      return;
    }
    else {
      if (!empty($items[0]['is_default'])) {

        // Check if this email is already subscribed.
        $email = mailchimp_lists_load_email($instance, $entity);
        $list_id = $field['settings']['mc_list_id'];
        $subscribed = mailchimp_is_subscribed($list_id, $email);
        if ($subscribed) {

          // The mail address is already subscribed. Do not overwrite the subscription.
          return;
        }
        $choices = $instance['default_value'][0];
      }
    }

    // Automatically subscribe if the field is configured to hide the Subscribe
    // checkbox and at least one interest group checkbox has been checked.
    if ($instance['settings']['show_interest_groups'] && $instance['settings']['hide_subscribe_checkbox']) {
      if (!empty($choices['interest_groups'])) {
        $subscribe_from_interest_groups = FALSE;
        foreach ($choices['interest_groups'] as $group_id => $interests) {
          foreach ($interests as $interest_id => $value) {
            if (!empty($value)) {
              $subscribe_from_interest_groups = TRUE;
              continue;
            }
          }
        }
        $choices['subscribe'] = $subscribe_from_interest_groups;
      }
    }
    mailchimp_lists_process_subscribe_form_choices($choices, $instance, $field, $entity);
  }
}