addressfield.tokens.inc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 (addressfield_get_address_fields() as $field_name => $field) {
  87. foreach (array($field_name, strtr($field_name, '_', '-')) as $name) {
  88. foreach ($data['tokens'] as $group => $token) {
  89. if (isset($data['tokens'][$group][$name]) && is_array($data['tokens'][$group][$name])) {
  90. // Set the token type for the field to use the addressfield child tokens.
  91. $data['tokens'][$group][$name]['type'] = 'address-field';
  92. }
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * Implements hook_tokens().
  99. */
  100. function addressfield_tokens($type, $tokens, array $data = array(), array $options = array()) {
  101. if (isset($options['language'])) {
  102. $language_code = $options['language']->language;
  103. }
  104. else {
  105. $language_code = LANGUAGE_NONE;
  106. }
  107. $sanitize = !empty($options['sanitize']);
  108. $replacements = array();
  109. // If we're generating tokens for an address field, extract the address data
  110. // from the field value array and generate the necessary replacements.
  111. if ($type == 'address-field' && !empty($data['address-field'][$language_code]) && is_array($data['address-field'][$language_code])) {
  112. $address = reset($data['address-field'][$language_code]);
  113. foreach ($tokens as $name => $original) {
  114. switch ($name) {
  115. case 'country':
  116. $countries = _addressfield_country_options_list();
  117. $replacements[$original] = $sanitize ? check_plain($countries[$address['country']]) : $countries[$address['country']];
  118. break;
  119. case 'country-code':
  120. $replacements[$original] = $sanitize ? check_plain($address['country']) : $address['country'];
  121. break;
  122. case 'administrative-area':
  123. // If we received format handlers in the data array, generate the form
  124. // for the address field to see if the administrative area should be
  125. // expanded from an abbreviation to a related name.
  126. $administrative_area = $address['administrative_area'];
  127. if (!empty($data['format_handlers'])) {
  128. $form = addressfield_generate($address, $data['format_handlers'], array('mode' => 'form'));
  129. if (!empty($form['locality_block']['administrative_area']['#options'][$administrative_area])) {
  130. $administrative_area = $form['locality_block']['administrative_area']['#options'][$administrative_area];
  131. }
  132. }
  133. $replacements[$original] = $sanitize ? check_plain($administrative_area) : $administrative_area;
  134. break;
  135. case 'administrative-area-raw':
  136. $replacements[$original] = $sanitize ? check_plain($address['administrative_area']) : $address['administrative_area'];
  137. break;
  138. case 'locality':
  139. $replacements[$original] = $sanitize ? check_plain($address['locality']) : $address['locality'];
  140. break;
  141. case 'postal-code':
  142. $replacements[$original] = $sanitize ? check_plain($address['postal_code']) : $address['postal_code'];
  143. break;
  144. case 'thoroughfare':
  145. $replacements[$original] = $sanitize ? check_plain($address['thoroughfare']) : $address['thoroughfare'];
  146. break;
  147. case 'premise':
  148. $replacements[$original] = $sanitize ? check_plain($address['premise']) : $address['premise'];
  149. break;
  150. case 'sub_premise':
  151. $replacements[$original] = $sanitize ? check_plain($address['sub_premise']) : $address['sub_premise'];
  152. break;
  153. case 'organisation':
  154. $replacements[$original] = $sanitize ? check_plain($address['organisation_name']) : $address['organisation_name'];
  155. break;
  156. case 'name-line':
  157. $replacements[$original] = $sanitize ? check_plain($address['name_line']) : $address['name_line'];
  158. break;
  159. case 'first-name':
  160. $replacements[$original] = $sanitize ? check_plain($address['first_name']) : $address['first_name'];
  161. break;
  162. case 'last-name':
  163. $replacements[$original] = $sanitize ? check_plain($address['last_name']) : $address['last_name'];
  164. break;
  165. // See: http://kb.mailchimp.com/article/how-do-i-format-my-list-fields-to-import-them
  166. case 'format-mailchimp':
  167. $components = array();
  168. foreach (array('thoroughfare', 'premise', 'locality', 'administrative_area', 'postal_code', 'country') as $component) {
  169. if (!empty($address[$component])) {
  170. $components[] = $address[$component];
  171. }
  172. }
  173. $format_mailchimp = implode(' ', $components);
  174. $replacements[$original] = $sanitize ? check_plain($format_mailchimp) : $format_mailchimp;
  175. break;
  176. }
  177. }
  178. }
  179. // The Token module extends direct token generation by using a generic entity
  180. // token generation process. Since we intend to overwrite the default Token
  181. // module implementation of address field tokens, we use this generic token
  182. // generation process to find and replace address field tokens on relevant
  183. // entities. This ensures our tokens aren't overwritten by the Token module
  184. // and helps us avoid having to do the entity detection ourselves.
  185. if ($type == 'entity') {
  186. // Loop over the address fields defined on the site.
  187. foreach (addressfield_get_address_fields($data['entity_type']) as $field_name => $field) {
  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. foreach (array($field_name, strtr($field_name, '_', '-')) as $prefix) {
  196. // If there are any address field tokens in the token list...
  197. $addressfield_tokens = token_find_with_prefix($tokens, $prefix);
  198. if (!$addressfield_tokens) {
  199. continue;
  200. }
  201. if (property_exists($data['entity'], $field_name)) {
  202. // Generate the necessary address field tokens for the entity.
  203. $replacements += token_generate('address-field', $addressfield_tokens, array('address-field' => $data['entity']->$field_name, 'format_handlers' => $format_handlers), $options);
  204. }
  205. }
  206. }
  207. }
  208. return $replacements;
  209. }