addressfield_tokens.tokens.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. // $Id$
  3. /*
  4. * Copyright © 2012 New Signature
  5. *
  6. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  7. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  8. * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
  9. * You can contact New Signature by electronic mail at labs@newsignature.com -or- by U.S. Postal Service at 1100 H St. NW, Suite 940, Washington, DC 20005.
  10. */
  11. /**
  12. * @file Provides token replacements for address fields.
  13. */
  14. /**
  15. * Implements hook_token_info_alter().
  16. *
  17. * Attaches tokens to all addressfield properties. The default names of each
  18. * addressfield component can be altered by administrators according to the site's locale.
  19. *
  20. * @param array $info The existing token info.
  21. */
  22. function addressfield_tokens_token_info_alter(&$info) {
  23. // Define the address field token types
  24. $info['types']['addressfield'] = array(
  25. 'name' => t('Address field'),
  26. 'description' => t('An address associated with an entity.'),
  27. 'needs-data' => 'addressfield',
  28. );
  29. // Add tokens for each component of the address
  30. $info['tokens'] += array( 'addressfield' => array() );
  31. $props = addressfield_data_property_info();
  32. $names = addressfield_tokens_property_names();
  33. $params = array(
  34. '@default_country' => addressfield_tokens_default_country(),
  35. );
  36. foreach ($props as $field => $data) {
  37. $fieldtoken = str_replace('_', '-', $field);
  38. $name = '';
  39. $descr = '';
  40. if (!empty($names[$field])) {
  41. $name = $names[$field];
  42. $descr = $data['label'];
  43. }
  44. else {
  45. $name = $data['label'];
  46. $descr = $name;
  47. $matches = array();
  48. if (preg_match('/^(.*)\s+\(i\.e\.\s+(.*)\)$/', $name, $matches)) {
  49. $name = $matches[1];
  50. $descr = $matches[2];
  51. }
  52. }
  53. $info['tokens']['addressfield'][$fieldtoken] = array(
  54. 'name' => $name,
  55. 'description' => $descr,
  56. 'type' => 'text',
  57. );
  58. $params['@' . $field] = $name;
  59. }
  60. $info['tokens']['addressfield']['administrative-area']['name'] .= ' (abbreviation)';
  61. $info['tokens']['addressfield']['country']['name'] .= ' (abbreviation)';
  62. // Add tokens for the formatted address and text-only version.
  63. $info['tokens']['addressfield'] += array(
  64. 'full' => array(
  65. 'name' => t('Formatted address'),
  66. 'description' => t('The full formatted address.'),
  67. 'type' => 'text',
  68. ),
  69. 'text' => array(
  70. 'name' => t('Text-only address'),
  71. 'description' => t('The full address with line breaks but no formatting.'),
  72. 'type' => 'text',
  73. ),
  74. 'city-state' => array(
  75. 'name' => t('City, State'),
  76. 'description' => t('@locality and @administrative_area separated by commas (and @country if outside @default_country)', $params),
  77. 'type' => 'text',
  78. ),
  79. 'state-name' => array(
  80. 'name' => t('@administrative_area (full name)', $params),
  81. 'description' => t('The full name of the @administrative_area', $params),
  82. 'type' => 'text',
  83. ),
  84. 'country-name' => array(
  85. 'name' => t('@country (full name)', $params),
  86. 'description' => t('The full name of the @country', $params),
  87. 'type' => 'text',
  88. ),
  89. );
  90. // Add user tokens that are useful for MailChimp.
  91. if (module_exists('mailchimp')) {
  92. $info['tokens']['addressfield'] += array(
  93. 'mc-address' => array(
  94. 'name' => t('MailChimp Address'),
  95. 'description' => t('A full address formatted for integration with MailChimp.'),
  96. 'type' => 'text',
  97. ),
  98. );
  99. }
  100. // Attach tokens to all address fields
  101. $valid_types = entity_token_types();
  102. $entity_info = entity_get_info();
  103. foreach ($valid_types as $token_type => $type) {
  104. foreach (entity_get_all_property_info($type) as $name => $property) {
  105. $name = str_replace('_', '-', $name);
  106. if (!isset($info['tokens'][$token_type][$name]) && isset($property['type']) && $property['type'] == 'addressfield') {
  107. $info['tokens'][$token_type][$name] = array(
  108. 'name' => $property['label'],
  109. 'type' => 'addressfield',
  110. 'description' => isset($property['description']) ? $property['description'] : t('Address field'),
  111. );
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Implements hook_tokens().
  118. *
  119. * @param string $type The type of tokens to replace. We are looking for 'addressfield', but can also chain
  120. * addressfields onto entities that have addressfields as properties.
  121. * @param array $tokens The tokens to replace.
  122. * @param array $data The data to use as replacements. We are looking for an 'addressfield' property.
  123. * @param array $options Additional options for the tokenizer.
  124. *
  125. * @return array The replaced values.
  126. */
  127. function addressfield_tokens_tokens($type, $tokens, array $data = array(), array $options = array()) {
  128. $url_options = array();
  129. if (isset($options['language'])) {
  130. $url_options['language'] = $options['language'];
  131. $language_code = $options['language']->language;
  132. }
  133. else {
  134. $language_code = LANGUAGE_NONE;
  135. }
  136. $sanitize = !empty($options['sanitize']);
  137. $replacements = array();
  138. // Process address field tokens
  139. if ($type == 'addressfield' && !empty($data['addressfield'])) {
  140. foreach ($tokens as $name => $original) {
  141. $name = str_replace('-', '_', $name);
  142. $address = $data['addressfield'];
  143. // If the address field exists, use it.
  144. if (isset($address[$name])) {
  145. $replacements[$original] = $address[$name];
  146. }
  147. else {
  148. // Otherwise, it's a special token
  149. switch ($name) {
  150. case 'full':
  151. $render = addressfield_generate($address, array('address'), array(
  152. 'mode' => 'render',
  153. ));
  154. $replacements[$original] = drupal_render($render);
  155. break;
  156. case 'text':
  157. $out = array();
  158. if (!empty($address['thoroughfare'])) {
  159. $out[0] = $address['thoroughfare'];
  160. }
  161. $out[1] = array();
  162. if (!empty($address['locality'])) {
  163. $out[1][] = $address['locality'];
  164. }
  165. if (!empty($address['administrative_area'])) {
  166. $out[1][] = $address['administrative_area'];
  167. }
  168. $out[1] = implode(', ', $out[1]);
  169. if (!empty($address['postal_code'])) {
  170. $out[1] .= ' ' . $address['postal_code'];
  171. }
  172. if (!empty($address['country']) && $address['country'] != addressfield_tokens_default_country()) {
  173. $out[2] = _addressfield_tokens_country($address['country']);
  174. }
  175. $replacements[$original] = implode("\n", $out);
  176. break;
  177. case 'city_state':
  178. $out = array();
  179. if (!empty($address['locality'])) {
  180. $out[] = $address['locality'];
  181. }
  182. if (!empty($address['administrative_area'])) {
  183. $out[] = $address['administrative_area'];
  184. }
  185. if (!empty($address['country']) && $address['country'] != addressfield_tokens_default_country()) {
  186. $out[] = _addressfield_tokens_country($address['country']);
  187. }
  188. $replacements[$original] = implode(", ", $out);
  189. break;
  190. case 'state_name':
  191. if (!empty($address['administrative_area']) && !empty($address['country'])) {
  192. $replacements[$original] = _addressfield_tokens_state($address['country'], $address['administrative_area']);
  193. }
  194. break;
  195. case 'country_name':
  196. if (!empty($address['country'])) {
  197. $replacements[$original] = _addressfield_tokens_country($address['country']);
  198. }
  199. break;
  200. case 'mc_address':
  201. $address_components = array('thoroughfare', 'premise', 'locality', 'administrative_area', 'postal_code', 'country');
  202. $mc_address = array();
  203. foreach ($address_components as $component) {
  204. if (!empty($address[$component])) {
  205. $mc_address[] = check_plain($address[$component]);
  206. }
  207. }
  208. // MailChimp requires the address to be a string of double-space
  209. // delimited address fields. (http://kb.mailchimp.com/article/how-do-i-set-up-the-address-field-type-for-import)
  210. $replacements[$original] = !empty($mc_address) ? implode(' ', $mc_address) : '';
  211. break;
  212. }
  213. }
  214. }
  215. if (!isset($replacements[$original])) {
  216. $replacements[$original] = '';
  217. }
  218. }
  219. else {
  220. $token_types = entity_token_types();
  221. $info = token_info();
  222. if (isset($info['tokens'][$type])) {
  223. // Otherwise, chain address fields attached to other entities
  224. foreach ($info['tokens'][$type] as $name => $token_info) {
  225. if (isset($token_info['type']) && $token_info['type'] == 'addressfield') {
  226. if ($chained_tokens = token_find_with_prefix($tokens, $name)) {
  227. $wrapper = !isset($wrapper) ? _entity_token_wrap_data($type, $token_types[$type], $data[$type], $options) : $wrapper;
  228. $property_name = str_replace('-', '_', $name);
  229. try {
  230. $address = $wrapper->$property_name->value();
  231. $replacements += token_generate('addressfield', $chained_tokens, array('addressfield' => $address), $options);
  232. }
  233. catch (EntityMetadataWrapperException $e) {
  234. // The property doesn't exist, so skip it.
  235. $replacements[$original] = '';
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. return $replacements;
  243. }