You are here

function mailchimp_mergevars_populate in Mailchimp 7.4

Same name and namespace in other branches
  1. 7.5 mailchimp.module \mailchimp_mergevars_populate()
  2. 7.3 mailchimp.module \mailchimp_mergevars_populate()

Replace an array of merge field placeholder tokens with their values.

Parameters

array $mergefields: An array of merge fields and token place holders to be expanded. The key of the array is the Mailchimp field name, the value is the token that should be expanded.

object $entity: The entity that contains the values to use for token replacement.

string $entity_type: The entity type of the entity being used.

string $list_id:

Return value

mixed Associative array of Mailchimp fields with values taken from the provided entity.

2 calls to mailchimp_mergevars_populate()
_mailchimp_lists_mergevars_populate in modules/mailchimp_lists/mailchimp_lists.module
Helper function to complete a Mailchimp-API-ready mergevars array.
_mailchimp_lists_subscription_has_changed in modules/mailchimp_lists/mailchimp_lists.module
Helper function to avoid sending superfluous updates to Mailchimp.

File

./mailchimp.module, line 1801
Mailchimp module.

Code

function mailchimp_mergevars_populate($mergefields, $entity, $entity_type, $list_id = NULL) {

  // Get mergevar settings from the list.
  $list_mergevars = array();
  if (!empty($list_id)) {
    $list = mailchimp_get_list($list_id, FALSE);
    if (!empty($list) && isset($list->mergevars)) {

      // Create array of mergevars indexed by tag. Used to map to mergefields.
      foreach ($list->mergevars as $list_mergevar) {
        $list_mergevars[$list_mergevar->tag] = $list_mergevar;
      }
    }
  }
  $mergevars = drupal_static(__FUNCTION__, array());
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
  if (!isset($mergevars[$bundle . ':' . $id])) {
    foreach ($mergefields as $key => $token) {
      $mergevar_value = token_replace($token, array(
        $entity_type => $entity,
      ), array(
        'clear' => TRUE,
      ));
      if (isset($list_mergevars[$key])) {
        if ($list_mergevars[$key]->type == 'date') {
          $mergevar_value = mailchimp_format_date($mergevar_value);
        }
        elseif ($list_mergevars[$key]->type == 'phone') {
          $mergevar_value = mailchimp_format_phone_number($mergevar_value);
        }
      }
      if (!empty($mergevar_value)) {
        $mergevars[$bundle . ':' . $id][$key] = $mergevar_value;
      }
    }
  }
  return $mergevars[$bundle . ':' . $id];
}