You are here

function mailchimp_lists_load_email in Mailchimp 7.4

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

Helper function to check if a valid email is configured for an entity field.

Returns an email address or FALSE.

7 calls to mailchimp_lists_load_email()
mailchimp_lists_add_to_segment_action in modules/mailchimp_lists/mailchimp_lists.module
Action function for the Add To Segment action.
mailchimp_lists_field_formatter_view in modules/mailchimp_lists/includes/mailchimp_lists.field.inc
Implements hook_field_formatter_view().
mailchimp_lists_field_get in modules/mailchimp_lists/includes/mailchimp_lists.field.inc
Entity field data callback for Mailchimp subscription fields.
mailchimp_lists_field_widget_form in modules/mailchimp_lists/includes/mailchimp_lists.field.inc
Implements hook_field_widget_form().
mailchimp_lists_process_subscribe_form_choices in modules/mailchimp_lists/mailchimp_lists.module
Processor for various list form submissions.

... See full list

File

modules/mailchimp_lists/mailchimp_lists.module, line 80

Code

function mailchimp_lists_load_email($instance, $entity, $log_errors = TRUE) {

  // Load original email if it exists.
  if (isset($entity->original)) {
    $email = mailchimp_lists_load_email($instance, $entity->original, $log_errors);
    if ($email) {
      return $email;
    }
  }
  if (!isset($instance['settings']['mergefields']['EMAIL'])) {
    if ($log_errors) {
      watchdog('mailchimp_lists', 'Mailchimp Lists field "@field" on @entity -> @bundle has no EMAIL field configured, subscription actions cannot take place.', array(
        '@field' => $instance['field_name'],
        '@entity' => $instance['entity_type'],
        '@bundle' => $instance['bundle'],
      ), WATCHDOG_NOTICE);
    }
    return FALSE;
  }

  // This function can be called when creating a form for a new entity, in which
  // case there will be no existing entity to inspect in order to figure out
  // what the email address is. For example, the user registration form. So we
  // need to skip the rest of this code if there is no ID for the entity.
  list($id, $vid, $bundle) = entity_extract_ids($instance['entity_type'], $entity);
  if (!isset($id) || $id === 0) {
    return FALSE;
  }

  // Expand the token used in the email merge field.
  $tokens = array();
  foreach ($instance['settings']['mergefields'] as $key => $token) {
    $tokens[$key] = token_replace($token, array(
      $instance['entity_type'] => $entity,
    ), array(
      'clear' => TRUE,
    ));
  }
  $email = $tokens['EMAIL'];
  if (valid_email_address($email)) {
    return $email;
  }
  else {
    return FALSE;
  }
}