You are here

function addressfield_tokens in Address Field 7

Implements hook_tokens().

File

./addressfield.tokens.inc, line 108
Token module integration.

Code

function addressfield_tokens($type, $tokens, array $data = array(), array $options = array()) {
  if (isset($options['language'])) {
    $language_code = $options['language']->language;
  }
  else {
    $language_code = LANGUAGE_NONE;
  }
  $sanitize = !empty($options['sanitize']);
  $replacements = array();

  // If we're generating tokens for an address field, extract the address data
  // from the field value array and generate the necessary replacements.
  if ($type == 'address-field' && !empty($data['address-field'][$language_code]) && is_array($data['address-field'][$language_code])) {
    $address = reset($data['address-field'][$language_code]);
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'country':
          $countries = _addressfield_country_options_list();
          $replacements[$original] = $sanitize ? check_plain($countries[$address['country']]) : $countries[$address['country']];
          break;
        case 'country-code':
        case 'country_code':
          $replacements[$original] = $sanitize ? check_plain($address['country']) : $address['country'];
          break;
        case 'administrative-area':
        case 'administrative_area':

          // If we received format handlers in the data array, generate the form
          // for the address field to see if the administrative area should be
          // expanded from an abbreviation to a related name.
          $administrative_area = $address['administrative_area'];
          if (!empty($data['format_handlers'])) {
            $form = addressfield_generate($address, $data['format_handlers'], array(
              'mode' => 'form',
            ));
            if (!empty($form['locality_block']['administrative_area']['#options'][$administrative_area])) {
              $administrative_area = $form['locality_block']['administrative_area']['#options'][$administrative_area];
            }
          }
          $replacements[$original] = $sanitize ? check_plain($administrative_area) : $administrative_area;
          break;
        case 'administrative-area-raw':
        case 'administrative_area_raw':
          $replacements[$original] = $sanitize ? check_plain($address['administrative_area']) : $address['administrative_area'];
          break;
        case 'locality':
          $replacements[$original] = $sanitize ? check_plain($address['locality']) : $address['locality'];
          break;
        case 'postal-code':
        case 'postal_code':
          $replacements[$original] = $sanitize ? check_plain($address['postal_code']) : $address['postal_code'];
          break;
        case 'thoroughfare':
          $replacements[$original] = $sanitize ? check_plain($address['thoroughfare']) : $address['thoroughfare'];
          break;
        case 'premise':
          $replacements[$original] = $sanitize ? check_plain($address['premise']) : $address['premise'];
          break;
        case 'sub_premise':
        case 'sub-premise':
          $replacements[$original] = $sanitize ? check_plain($address['sub_premise']) : $address['sub_premise'];
          break;
        case 'organisation':
          $replacements[$original] = $sanitize ? check_plain($address['organisation_name']) : $address['organisation_name'];
          break;
        case 'name-line':
        case 'name_line':
          $replacements[$original] = $sanitize ? check_plain($address['name_line']) : $address['name_line'];
          break;
        case 'first-name':
        case 'first_name':
          $replacements[$original] = $sanitize ? check_plain($address['first_name']) : $address['first_name'];
          break;
        case 'last-name':
        case 'last_name':
          $replacements[$original] = $sanitize ? check_plain($address['last_name']) : $address['last_name'];
          break;

        // See: http://kb.mailchimp.com/article/how-do-i-format-my-list-fields-to-import-them
        case 'format-mailchimp':
        case 'format_mailchimp':
          $components = array();
          foreach (array(
            'thoroughfare',
            'premise',
            'locality',
            'administrative_area',
            'postal_code',
            'country',
          ) as $component) {
            if (!empty($address[$component])) {
              $components[] = $address[$component];
            }
          }
          $format_mailchimp = implode('  ', $components);
          $replacements[$original] = $sanitize ? check_plain($format_mailchimp) : $format_mailchimp;
          break;
      }
    }
  }

  // The Token module extends direct token generation by using a generic entity
  // token generation process. Since we intend to overwrite the default Token
  // module implementation of address field tokens, we use this generic token
  // generation process to find and replace address field tokens on relevant
  // entities. This ensures our tokens aren't overwritten by the Token module
  // and helps us avoid having to do the entity detection ourselves.
  if ($type == 'entity') {
    $entity_type = $data['entity_type'];
    $entity = $data['entity'];
  }
  elseif (!module_exists('token')) {
    $entity_type = addresssfield_get_token_entity_mapping('token', $type);
    if (empty($entity_type)) {
      return $replacements;
    }
    $entity = $data[$type];
  }
  if (!isset($entity_type)) {
    return $replacements;
  }

  // Loop over the address fields defined on the site.
  foreach (addressfield_get_address_fields($entity_type) as $field_name => $field) {

    // If the current field is on the matching entity type...
    if (!empty($field['bundles'][$entity_type])) {

      // Extract the format handlers selected in a representative instance
      // settings form for use in formatting tokens.
      $instance = field_info_instance($entity_type, $field_name, reset($field['bundles'][$entity_type]));
      $format_handlers = $instance['widget']['settings']['format_handlers'];
    }
    foreach (array(
      $field_name,
      strtr($field_name, '_', '-'),
    ) as $prefix) {

      // If there are any address field tokens in the token list...
      $addressfield_tokens = token_find_with_prefix($tokens, $prefix);
      if (!$addressfield_tokens || !property_exists($entity, $field_name)) {
        continue;
      }

      // Generate the necessary address field tokens for the entity.
      $replacements += token_generate('address-field', $addressfield_tokens, array(
        'address-field' => $entity->{$field_name},
        'format_handlers' => $format_handlers,
      ), $options);
    }
  }
  return $replacements;
}