addressfield.tokens.inc 8.9 KB

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