You are here

function addressfield_phone_tokens in Address Field Phone 7

Implements hook_tokens().

Replace phone tokens with actual values from an address field.

File

./addressfield_phone.module, line 192
Add additional phone fields to addressfield.

Code

function addressfield_phone_tokens($type, $tokens, $data = array(), $options = array()) {
  if (isset($options['language'])) {
    $language_code = $options['language']->language;
  }
  else {
    $language_code = LANGUAGE_NONE;
  }
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  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) {
      if ($name == 'phone-number' || $name == 'phone-number-extension' || $name == 'mobile-number' || $name == 'fax-number') {

        // Token names are just the field properties with dashes, so we can skip
        // having to do any sort of if/switching on them.
        $property = str_replace('-', '_', $name);
        $replacements[$original] = $sanitize ? check_plain($address[$property]) : $address[$property];
      }
    }
  }
  return $replacements;
}