1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Token module integration.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
/**
|
9 |
|
|
* Implements hook_token_info().
|
10 |
|
|
*/
|
11 |
|
|
function addressfield_token_info() {
|
12 |
|
|
$type = array(
|
13 |
|
|
'name' => t('Address field'),
|
14 |
|
|
'description' => t('Tokens related to address field values and their components.'),
|
15 |
|
|
'needs-data' => 'address-field',
|
16 |
|
|
'field' => TRUE,
|
17 |
|
|
);
|
18 |
|
|
|
19 |
|
|
// Define tokens for the various components of addresses supported through the
|
20 |
|
|
// user interface along with two helper tokens for country and administrative
|
21 |
|
|
// area to distinguish between names and abbreviations.
|
22 |
|
|
$info['country'] = array(
|
23 |
|
|
'name' => t('Country name'),
|
24 |
|
|
'description' => t('The full name of the country.'),
|
25 |
|
|
);
|
26 |
|
|
$info['country-code'] = array(
|
27 |
|
|
'name' => t('Country code'),
|
28 |
|
|
'description' => t('The two letter ISO country code.'),
|
29 |
|
|
);
|
30 |
|
|
$info['administrative-area'] = array(
|
31 |
|
|
'name' => t('Administrative area (i.e. State/Province)'),
|
32 |
|
|
'description' => t('The administrative area value, expanded to the full name if applicable.'),
|
33 |
|
|
);
|
34 |
|
|
$info['administrative-area-raw'] = array(
|
35 |
|
|
'name' => t('Administrative area (raw value)'),
|
36 |
|
|
'description' => t('The raw administrative area value.'),
|
37 |
|
|
);
|
38 |
|
|
$info['locality'] = array(
|
39 |
|
|
'name' => t('Locality (i.e. City)'),
|
40 |
|
|
'description' => t('The locality value.'),
|
41 |
|
|
);
|
42 |
|
|
$info['postal-code'] = array(
|
43 |
|
|
'name' => t('Postal code'),
|
44 |
|
|
'description' => t('The postal code value.'),
|
45 |
|
|
);
|
46 |
|
|
$info['thoroughfare'] = array(
|
47 |
|
|
'name' => t('Thoroughfare (i.e. Street address)'),
|
48 |
|
|
'description' => t('The thoroughfare value.'),
|
49 |
|
|
);
|
50 |
|
|
$info['premise'] = array(
|
51 |
|
|
'name' => t('Premise (i.e. Street address)'),
|
52 |
|
|
'description' => t('The thoroughfare value.'),
|
53 |
|
|
);
|
54 |
|
|
$info['organisation'] = array(
|
55 |
|
|
'name' => t('Organisation'),
|
56 |
|
|
'description' => t('The organisation name value.'),
|
57 |
|
|
);
|
58 |
|
|
$info['name-line'] = array(
|
59 |
|
|
'name' => t('Full name'),
|
60 |
|
|
'description' => t('The name line value of the address.'),
|
61 |
|
|
);
|
62 |
|
|
$info['first-name'] = array(
|
63 |
|
|
'name' => t('First name'),
|
64 |
|
|
'description' => t('The first name value.'),
|
65 |
|
|
);
|
66 |
|
|
$info['last-name'] = array(
|
67 |
|
|
'name' => t('Last name'),
|
68 |
|
|
'description' => t('The last name value.'),
|
69 |
|
|
);
|
70 |
|
|
|
71 |
|
|
// Add a helper token to format addresses as expected by MailChimp.
|
72 |
|
|
$info['format-mailchimp'] = array(
|
73 |
|
|
'name' => t('Address formatted for MailChimp'),
|
74 |
|
|
'description' => t('The full address formatted for import into MailChimp.'),
|
75 |
|
|
);
|
76 |
|
|
|
77 |
|
|
return array(
|
78 |
|
|
'types' => array('address-field' => $type),
|
79 |
|
|
'tokens' => array('address-field' => $info),
|
80 |
|
|
);
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
/**
|
84 |
|
|
* Implements hook_token_info_alter().
|
85 |
|
|
*/
|
86 |
|
|
function addressfield_token_info_alter(&$data) {
|
87 |
|
|
// Loop over every address field on the site.
|
88 |
|
|
foreach (array_filter(field_info_field_map(), 'addressfield_field_map_filter') as $field_name => $field) {
|
89 |
|
|
foreach ($data['tokens'] as $group => $token){
|
90 |
|
|
if (isset($data['tokens'][$group][$field_name]) && is_array($data['tokens'][$group][$field_name])) {
|
91 |
|
|
// Set the token type for the field to use the addressfield child tokens.
|
92 |
|
|
$data['tokens'][$group][$field_name]['type'] = 'address-field';
|
93 |
|
|
}
|
94 |
|
|
}
|
95 |
|
|
}
|
96 |
|
|
}
|
97 |
|
|
|
98 |
|
|
/**
|
99 |
|
|
* Implements hook_tokens().
|
100 |
|
|
*/
|
101 |
|
|
function addressfield_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
102 |
|
|
if (isset($options['language'])) {
|
103 |
|
|
$language_code = $options['language']->language;
|
104 |
|
|
}
|
105 |
|
|
else {
|
106 |
|
|
$language_code = LANGUAGE_NONE;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
$sanitize = !empty($options['sanitize']);
|
110 |
|
|
|
111 |
|
|
$replacements = array();
|
112 |
|
|
|
113 |
|
|
// If we're generating tokens for an address field, extract the address data
|
114 |
|
|
// from the field value array and generate the necessary replacements.
|
115 |
|
|
if ($type == 'address-field' && !empty($data['address-field'][$language_code]) && is_array($data['address-field'][$language_code])) {
|
116 |
|
|
$address = reset($data['address-field'][$language_code]);
|
117 |
|
|
|
118 |
|
|
foreach ($tokens as $name => $original) {
|
119 |
|
|
switch ($name) {
|
120 |
|
|
case 'country':
|
121 |
|
|
$countries = _addressfield_country_options_list();
|
122 |
|
|
$replacements[$original] = $sanitize ? check_plain($countries[$address['country']]) : $countries[$address['country']];
|
123 |
|
|
break;
|
124 |
|
|
|
125 |
|
|
case 'country-code':
|
126 |
|
|
$replacements[$original] = $sanitize ? check_plain($address['country']) : $address['country'];
|
127 |
|
|
break;
|
128 |
|
|
|
129 |
|
|
case 'administrative-area':
|
130 |
|
|
// If we received format handlers in the data array, generate the form
|
131 |
|
|
// for the address field to see if the administrative area should be
|
132 |
|
|
// expanded from an abbreviation to a related name.
|
133 |
|
|
$administrative_area = $address['administrative_area'];
|
134 |
|
|
|
135 |
|
|
if (!empty($data['format_handlers'])) {
|
136 |
|
|
$form = addressfield_generate($address, $data['format_handlers'], array('mode' => 'form'));
|
137 |
|
|
|
138 |
|
|
if (!empty($form['locality_block']['administrative_area']['#options'][$administrative_area])) {
|
139 |
|
|
$administrative_area = $form['locality_block']['administrative_area']['#options'][$administrative_area];
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
$replacements[$original] = $sanitize ? check_plain($administrative_area) : $administrative_area;
|
144 |
|
|
break;
|
145 |
|
|
|
146 |
|
|
case 'administrative-area-raw':
|
147 |
|
|
$replacements[$original] = $sanitize ? check_plain($address['administrative_area']) : $address['administrative_area'];
|
148 |
|
|
break;
|
149 |
|
|
|
150 |
|
|
case 'locality':
|
151 |
|
|
$replacements[$original] = $sanitize ? check_plain($address['locality']) : $address['locality'];
|
152 |
|
|
break;
|
153 |
|
|
|
154 |
|
|
case 'postal-code':
|
155 |
|
|
$replacements[$original] = $sanitize ? check_plain($address['postal_code']) : $address['postal_code'];
|
156 |
|
|
break;
|
157 |
|
|
|
158 |
|
|
case 'thoroughfare':
|
159 |
|
|
$replacements[$original] = $sanitize ? check_plain($address['thoroughfare']) : $address['thoroughfare'];
|
160 |
|
|
break;
|
161 |
|
|
|
162 |
|
|
case 'premise':
|
163 |
|
|
$replacements[$original] = $sanitize ? check_plain($address['premise']) : $address['premise'];
|
164 |
|
|
break;
|
165 |
|
|
|
166 |
|
|
case 'organisation':
|
167 |
|
|
$replacements[$original] = $sanitize ? check_plain($address['organisation_name']) : $address['organisation_name'];
|
168 |
|
|
break;
|
169 |
|
|
|
170 |
|
|
case 'name-line':
|
171 |
|
|
$replacements[$original] = $sanitize ? check_plain($address['name_line']) : $address['name_line'];
|
172 |
|
|
break;
|
173 |
|
|
|
174 |
|
|
case 'first-name':
|
175 |
|
|
$replacements[$original] = $sanitize ? check_plain($address['first_name']) : $address['first_name'];
|
176 |
|
|
break;
|
177 |
|
|
|
178 |
|
|
case 'last-name':
|
179 |
|
|
$replacements[$original] = $sanitize ? check_plain($address['last_name']) : $address['last_name'];
|
180 |
|
|
break;
|
181 |
|
|
|
182 |
|
|
// See: http://kb.mailchimp.com/article/how-do-i-format-my-list-fields-to-import-them
|
183 |
|
|
case 'format-mailchimp':
|
184 |
|
|
$components = array();
|
185 |
|
|
|
186 |
|
|
foreach (array('thoroughfare', 'premise', 'locality', 'administrative_area', 'postal_code', 'country') as $component) {
|
187 |
|
|
if (!empty($address[$component])) {
|
188 |
|
|
$components[] = $address[$component];
|
189 |
|
|
}
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
$format_mailchimp = implode(' ', $components);
|
193 |
|
|
$replacements[$original] = $sanitize ? check_plain($format_mailchimp) : $format_mailchimp;
|
194 |
|
|
break;
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
}
|
198 |
|
|
|
199 |
|
|
// The Token module extends direct token generation by using a generic entity
|
200 |
|
|
// token generation process. Since we intend to overwrite the default Token
|
201 |
|
|
// module implementation of address field tokens, we use this generic token
|
202 |
|
|
// generation process to find and replace address field tokens on relevant
|
203 |
|
|
// entities. This ensures our tokens aren't overwritten by the Token module
|
204 |
|
|
// and helps us avoid having to do the entity detection ourselves.
|
205 |
|
|
if ($type == 'entity') {
|
206 |
|
|
// Loop over the address fields defined on the site.
|
207 |
|
|
foreach (array_filter(field_info_field_map(), 'addressfield_field_map_filter') as $field_name => $field) {
|
208 |
|
|
// If there are any address field tokens in the token list...
|
209 |
|
|
if ($addressfield_tokens = token_find_with_prefix($tokens, $field_name)) {
|
210 |
|
|
// If the current field is on the matching entity type...
|
211 |
|
|
if (!empty($field['bundles'][$data['entity_type']])) {
|
212 |
|
|
// Extract the format handlers selected in a representative instance
|
213 |
|
|
// settings form for use in formatting tokens.
|
214 |
|
|
$instance = field_info_instance($data['entity_type'], $field_name, reset($field['bundles'][$data['entity_type']]));
|
215 |
|
|
$format_handlers = $instance['widget']['settings']['format_handlers'];
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
// Generate the necessary address field tokens for the entity.
|
219 |
|
|
$replacements += token_generate('address-field', $addressfield_tokens, array('address-field' => $data['entity']->$field_name, 'format_handlers' => $format_handlers), $options);
|
220 |
|
|
}
|
221 |
|
|
}
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
return $replacements;
|
225 |
|
|
} |