You are here

function uc_addresses_token_replace in Ubercart Addresses 6.2

Replace tokens using uc_addresses tokens.

Parameters

string $text: The text to replace.

UcAddressesAddress $address: The address object that contains substitutions for tokens.

Return value

string Text with uc_addresses tokens replaced.

1 call to uc_addresses_token_replace()
uc_addresses_format_address in ./uc_addresses.module
Format an address by using tokens.

File

./uc_addresses.tokens.inc, line 20
Copy of functionality in token.module to allow to replace tokens of uc_addresses two levels deep.

Code

function uc_addresses_token_replace($text, UcAddressesAddress $address) {
  $type = 'uc_addresses';
  $object = $address;
  $options = array();
  token_include();
  $id = _token_get_id($type, $object);
  if ($id && isset($tokens[$type][$id])) {
    $tmp_tokens = $tokens[$type][$id];
  }
  else {
    $tmp_tokens = module_invoke_all('token_values', $type, $object, $options);
    $tokens[$type][$id] = $tmp_tokens;
  }

  // Special-case global tokens, as we always want to be able to process
  // those substitutions.
  if (!isset($tokens['global']['default'])) {
    $tokens['global']['default'] = module_invoke_all('token_values', 'global');
  }
  $all = array_merge($tokens['global']['default'], $tokens[$type][$id]);
  $result = new stdClass();
  $result->tokens = array_keys($all);
  $result->values = array_values($all);
  $tokens = token_prepare_tokens($result->tokens);
  return str_replace($tokens, $result->values, $text);
}