function addressfield_tokens_token_info_alter in Addressfield Tokens 7
Implements hook_token_info_alter().
Attaches tokens to all addressfield properties. The default names of each addressfield component can be altered by administrators according to the site's locale.
File
- ./
addressfield_tokens.tokens.inc, line 15 - Provides token replacements for address fields.
Code
function addressfield_tokens_token_info_alter(&$info) {
// Define the address field token types.
$info['types']['addressfield'] = array(
'name' => t('Address field'),
'description' => t('An address associated with an entity.'),
'needs-data' => 'addressfield',
);
// Add tokens for each component of the address.
$info['tokens'] += array(
'addressfield' => array(),
);
$props = addressfield_data_property_info();
$names = addressfield_tokens_property_names();
$params = array(
'@default_country' => addressfield_tokens_default_country(),
);
foreach ($props as $field => $data) {
$fieldtoken = str_replace('_', '-', $field);
if (!empty($names[$field])) {
$name = $names[$field];
$descr = $data['label'];
}
else {
$name = $data['label'];
$descr = $name;
$matches = array();
if (preg_match('/^(.*)\\s+\\(i\\.e\\.\\s+(.*)\\)$/', $name, $matches)) {
$name = $matches[1];
$descr = $matches[2];
}
}
$info['tokens']['addressfield'][$fieldtoken] = array(
'name' => $name,
'description' => $descr,
'type' => 'text',
);
$params['@' . $field] = $name;
}
$info['tokens']['addressfield']['administrative-area']['name'] .= ' (abbreviation)';
$info['tokens']['addressfield']['country']['name'] .= ' (abbreviation)';
// Add tokens for the formatted address and text-only version.
$info['tokens']['addressfield'] += array(
'full' => array(
'name' => t('Formatted address'),
'description' => t('The full formatted address.'),
'type' => 'text',
),
'text' => array(
'name' => t('Text-only address'),
'description' => t('The full address with line breaks but no formatting.'),
'type' => 'text',
),
'city-state' => array(
'name' => t('City, State'),
'description' => t('@locality and @administrative_area separated by commas (and @country if outside @default_country)', $params),
'type' => 'text',
),
'state-name' => array(
'name' => t('@administrative_area (full name)', $params),
'description' => t('The full name of the @administrative_area', $params),
'type' => 'text',
),
'country-name' => array(
'name' => t('@country (full name)', $params),
'description' => t('The full name of the @country', $params),
'type' => 'text',
),
);
// Add user tokens that are useful for MailChimp.
if (module_exists('mailchimp')) {
$info['tokens']['addressfield'] += array(
'mc-address' => array(
'name' => t('MailChimp Address'),
'description' => t('A full address formatted for integration with MailChimp.'),
'type' => 'text',
),
);
}
// Add extra text to webform submission values help.
if (array_key_exists('submission', $info['tokens'])) {
$info['tokens']['submission']['values']['description'] .= '
<div>
' . t('For addressfield components you can also choose individual elements of the address using the syntax field_key:element, For example:') . '
<ul>
<li>[submission:values:address:thoroughfare]</li>
<li>[submission:values:address:locality]</li>
<li>[submission:values:address:administrative_area]</li>
<li>[submission:values:address:postal_code]</li>
<li>[submission:values:address:country]</li>
</ul>
</div>';
}
// Attach tokens to all address fields.
$valid_types = entity_token_types();
foreach ($valid_types as $token_type => $type) {
foreach (entity_get_all_property_info($type) as $name => $property) {
$name = str_replace('_', '-', $name);
if (!isset($info['tokens'][$token_type][$name]) && isset($property['type']) && $property['type'] == 'addressfield') {
$info['tokens'][$token_type][$name] = array(
'name' => $property['label'],
'type' => 'addressfield',
'description' => isset($property['description']) ? $property['description'] : t('Address field'),
);
}
}
}
}