function addressfield_tokens_tokens in Addressfield Tokens 7
Implements hook_tokens().
File
- ./
addressfield_tokens.tokens.inc, line 135 - Provides token replacements for address fields.
Code
function addressfield_tokens_tokens($type, $tokens, array $data = array(), array $options = array()) {
$url_options = array();
// @todo: why are we setting $language_code and not using it?
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = LANGUAGE_NONE;
}
$sanitize = !empty($options['sanitize']);
$replacements = array();
$last_original = NULL;
// Process webform submission addressfield tokens.
if ($type == 'submission' && !empty($data['webform-submission'])) {
$submission = $data['webform-submission'];
$node = isset($data['node']) ? $data['node'] : node_load($submission->nid);
$value_tokens = token_find_with_prefix($tokens, 'values');
// Loop through the components of the webform looking for addressfield
// components with the expected form_key.
foreach ($node->webform['components'] as $cid => $component) {
if ($component['type'] == 'addressfield' && isset($submission->data[$cid][0])) {
$address = $submission->data[$cid][0];
if (is_string($address)) {
$address = unserialize($address);
}
$address_tokens = token_find_with_prefix($value_tokens, $component['form_key']);
foreach ($address_tokens as $token => $original) {
if (isset($address[$token])) {
$replacements[$original] = $sanitize ? filter_xss($address[$token]) : $address[$token];
}
}
}
}
}
elseif ($type == 'addressfield' && !empty($data['addressfield'])) {
foreach ($tokens as $name => $original) {
$last_original = $original;
$name = str_replace('-', '_', $name);
$address = $data['addressfield'];
// If the address field exists, use it.
if (isset($address[$name])) {
$replacements[$original] = $sanitize ? filter_xss($address[$name]) : $address[$name];
}
else {
// Otherwise, it's a special token.
switch ($name) {
case 'full':
$render = addressfield_generate($address, array(
'address',
), array(
'mode' => 'render',
));
$replacements[$original] = $sanitize ? filter_xss(drupal_render($render)) : drupal_render($render);
break;
case 'text':
$out = array();
if (!empty($address['thoroughfare'])) {
$out[0] = $address['thoroughfare'];
}
$out[1] = array();
if (!empty($address['locality'])) {
$out[1][] = $address['locality'];
}
if (!empty($address['administrative_area'])) {
$out[1][] = $address['administrative_area'];
}
$out[1] = implode(', ', $out[1]);
if (!empty($address['postal_code'])) {
$out[1] .= ' ' . $address['postal_code'];
}
if (!empty($address['country']) && $address['country'] != addressfield_tokens_default_country()) {
$out[2] = _addressfield_tokens_country($address['country']);
}
$replacements[$original] = $sanitize ? filter_xss(implode("\n", $out)) : implode("\n", $out);
break;
case 'city_state':
$out = array();
if (!empty($address['locality'])) {
$out[] = $address['locality'];
}
if (!empty($address['administrative_area'])) {
$out[] = $address['administrative_area'];
}
if (!empty($address['country']) && $address['country'] != addressfield_tokens_default_country()) {
$out[] = _addressfield_tokens_country($address['country']);
}
$replacements[$original] = $sanitize ? filter_xss(implode(", ", $out)) : implode(", ", $out);
break;
case 'state_name':
if (!empty($address['administrative_area']) && !empty($address['country'])) {
if ($sanitize) {
$replacements[$original] = filter_xss(addressfield_tokens_state($address['country'], $address['administrative_area']));
}
else {
$replacements[$original] = addressfield_tokens_state($address['country'], $address['administrative_area']);
}
}
break;
case 'country_name':
if (!empty($address['country'])) {
if ($sanitize) {
$replacements[$original] = filter_xss(_addressfield_tokens_country($address['country']));
}
else {
$replacements[$original] = _addressfield_tokens_country($address['country']);
}
}
break;
case 'mc_address':
$address_components = array(
'thoroughfare',
'premise',
'locality',
'administrative_area',
'postal_code',
'country',
);
$mc_address = array();
foreach ($address_components as $component) {
if (!empty($address[$component])) {
$mc_address[] = check_plain($address[$component]);
}
}
// MailChimp requires the address to be a string of double-space
// delimited address fields. (http://kb.mailchimp.com/article/how-do-i-set-up-the-address-field-type-for-import)
$replacements[$original] = !empty($mc_address) ? implode(' ', $mc_address) : '';
break;
}
}
}
if (!isset($replacements[$last_original])) {
$replacements[$last_original] = '';
}
}
else {
$token_types = entity_token_types();
$info = token_info();
if (isset($info['tokens'][$type])) {
// Otherwise, chain address fields attached to other entities.
foreach ($info['tokens'][$type] as $name => $token_info) {
if (isset($token_info['type']) && $token_info['type'] == 'addressfield') {
if ($chained_tokens = token_find_with_prefix($tokens, $name)) {
$wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, $token_types[$type], $data[$type], $options) : $wrapper;
$property_name = str_replace('-', '_', $name);
try {
$address = $wrapper->{$property_name}
->value();
$replacements += token_generate('addressfield', $chained_tokens, array(
'addressfield' => $address,
), $options);
} catch (EntityMetadataWrapperException $e) {
// The property doesn't exist, so skip it.
$replacements[$last_original] = '';
}
}
}
}
}
}
return $replacements;
}