You are here

function _mailchimp_lists_field_postsave in Mailchimp 7.3

Same name and namespace in other branches
  1. 7.5 modules/mailchimp_lists/includes/mailchimp_lists.field.inc \_mailchimp_lists_field_postsave()
  2. 7.4 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 640
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]) || !empty($item[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];
    }
    mailchimp_lists_process_subscribe_form_choices($choices, $instance, $field, $entity);
  }
}