address.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /**
  3. * @file
  4. * The default format for adresses.
  5. */
  6. $plugin = array(
  7. 'title' => t('Address form (country-specific)'),
  8. 'format callback' => 'addressfield_format_address_generate',
  9. 'type' => 'address',
  10. 'weight' => -100,
  11. );
  12. /**
  13. * Format callback.
  14. *
  15. * @see CALLBACK_addressfield_format_callback()
  16. */
  17. function addressfield_format_address_generate(&$format, $address, $context = array()) {
  18. // We start with a reasonable default: a simple block format suitable
  19. // for international shipping. We extend it with country-specific heuristics
  20. // below.
  21. // The street block.
  22. $format['street_block'] = array(
  23. '#type' => 'addressfield_container',
  24. '#attributes' => array('class' => array('street-block')),
  25. '#weight' => 0,
  26. );
  27. $format['street_block']['thoroughfare'] = array(
  28. '#title' => t('Address 1'),
  29. '#tag' => 'div',
  30. '#attributes' => array('class' => array('thoroughfare')),
  31. '#size' => 30,
  32. // The #required will be automatically set to FALSE when processing.
  33. '#required' => TRUE,
  34. );
  35. $format['street_block']['premise'] = array(
  36. '#title' => t('Address 2'),
  37. '#tag' => 'div',
  38. '#attributes' => array('class' => array('premise')),
  39. '#size' => 30,
  40. );
  41. $format['locality_block'] = array(
  42. '#type' => 'addressfield_container',
  43. '#attributes' => array('class' => array('addressfield-container-inline', 'locality-block', 'country-' . $address['country'])),
  44. '#weight' => 50,
  45. );
  46. $format['locality_block']['#attached']['css'][] = drupal_get_path('module', 'addressfield') . '/addressfield.css';
  47. $format['locality_block']['postal_code'] = array(
  48. '#title' => t('Postal Code'),
  49. '#size' => 10,
  50. '#required' => TRUE,
  51. '#attributes' => array('class' => array('postal-code')),
  52. );
  53. $format['locality_block']['locality'] = array(
  54. '#title' => t('City'),
  55. '#size' => 30,
  56. '#required' => TRUE,
  57. '#prefix' => ' ',
  58. '#attributes' => array('class' => array('locality')),
  59. );
  60. $format['country'] = array(
  61. '#title' => t('Country'),
  62. '#options' => _addressfield_country_options_list(),
  63. '#required' => TRUE,
  64. '#attributes' => array('class' => array('country')),
  65. '#weight' => 100,
  66. );
  67. // Those countries do not seem to have a relevant postal code.
  68. if (in_array($address['country'], array('AF', 'AG', 'AL', 'AO', 'BB', 'BI', 'BJ', 'BO', 'BS', 'BW', 'BZ', 'CF', 'CG', 'CM', 'CO', 'DJ', 'DM', 'EG', 'ER', 'FJ', 'GD', 'GH', 'GM', 'GQ', 'GY', 'IE', 'KI', 'KM', 'KP', 'KY', 'LC', 'LY', 'ML', 'MR', 'NA', 'NR', 'RW', 'SB', 'SC', 'SL', 'SR', 'ST', 'TD', 'TG', 'TL', 'TO', 'TT', 'TV', 'TZ', 'UG', 'VC', 'VU', 'WS', 'ZW'))) {
  69. unset($format['locality_block']['postal_code']);
  70. // Remove the prefix from the first widget of the block.
  71. $element_children = element_children($format['locality_block']);
  72. $first_child = reset($element_children);
  73. unset($format['locality_block'][$first_child]['#prefix']);
  74. }
  75. // Those countries generally use their administrative division in postal addresses.
  76. if (in_array($address['country'], array('AR', 'AU', 'BR', 'BS', 'BY', 'BZ', 'CA', 'CN', 'DO', 'EG', 'ES', 'FJ', 'FM', 'GB', 'HN', 'ID', 'IE', 'IN', 'IT', 'JO', 'JP', 'KI', 'KN', 'KR', 'KW', 'KY', 'KZ', 'MX', 'MY', 'MZ', 'NG', 'NI', 'NR', 'NZ', 'OM', 'PA', 'PF', 'PG', 'PH', 'PR', 'PW', 'RU', 'SM', 'SO', 'SR', 'SV', 'TH', 'TW', 'UA', 'US', 'UY', 'VE', 'VI', 'VN', 'YU', 'ZA'))) {
  77. $format['locality_block']['administrative_area'] = array(
  78. '#title' => t('State'),
  79. '#size' => 10,
  80. '#required' => TRUE,
  81. '#prefix' => ' ',
  82. '#attributes' => array('class' => array('state')),
  83. );
  84. }
  85. // A few contries have a well-known list of administrative divisions.
  86. if ($address['country'] == 'US') {
  87. $format['locality_block']['administrative_area']['#options'] = array(
  88. '' => t('--'),
  89. 'AL' => t('Alabama'),
  90. 'AK' => t('Alaska'),
  91. 'AZ' => t('Arizona'),
  92. 'AR' => t('Arkansas'),
  93. 'CA' => t('California'),
  94. 'CO' => t('Colorado'),
  95. 'CT' => t('Connecticut'),
  96. 'DE' => t('Delaware'),
  97. 'DC' => t('District Of Columbia'),
  98. 'FL' => t('Florida'),
  99. 'GA' => t('Georgia'),
  100. 'HI' => t('Hawaii'),
  101. 'ID' => t('Idaho'),
  102. 'IL' => t('Illinois'),
  103. 'IN' => t('Indiana'),
  104. 'IA' => t('Iowa'),
  105. 'KS' => t('Kansas'),
  106. 'KY' => t('Kentucky'),
  107. 'LA' => t('Louisiana'),
  108. 'ME' => t('Maine'),
  109. 'MD' => t('Maryland'),
  110. 'MA' => t('Massachusetts'),
  111. 'MI' => t('Michigan'),
  112. 'MN' => t('Minnesota'),
  113. 'MS' => t('Mississippi'),
  114. 'MO' => t('Missouri'),
  115. 'MT' => t('Montana'),
  116. 'NE' => t('Nebraska'),
  117. 'NV' => t('Nevada'),
  118. 'NH' => t('New Hampshire'),
  119. 'NJ' => t('New Jersey'),
  120. 'NM' => t('New Mexico'),
  121. 'NY' => t('New York'),
  122. 'NC' => t('North Carolina'),
  123. 'ND' => t('North Dakota'),
  124. 'OH' => t('Ohio'),
  125. 'OK' => t('Oklahoma'),
  126. 'OR' => t('Oregon'),
  127. 'PA' => t('Pennsylvania'),
  128. 'RI' => t('Rhode Island'),
  129. 'SC' => t('South Carolina'),
  130. 'SD' => t('South Dakota'),
  131. 'TN' => t('Tennessee'),
  132. 'TX' => t('Texas'),
  133. 'UT' => t('Utah'),
  134. 'VT' => t('Vermont'),
  135. 'VA' => t('Virginia'),
  136. 'WA' => t('Washington'),
  137. 'WV' => t('West Virginia'),
  138. 'WI' => t('Wisconsin'),
  139. 'WY' => t('Wyoming'),
  140. ' ' => t('--'),
  141. 'AS' => t('American Samoa'),
  142. 'FM' => t('Federated States of Micronesia'),
  143. 'GU' => t('Guam'),
  144. 'MH' => t('Marshall Islands'),
  145. 'MP' => t('Northern Mariana Islands'),
  146. 'PW' => t('Palau'),
  147. 'PR' => t('Puerto Rico'),
  148. 'VI' => t('Virgin Islands'),
  149. );
  150. $format['locality_block']['postal_code']['#title'] = t('ZIP Code');
  151. }
  152. else if ($address['country'] == 'IT') {
  153. $format['locality_block']['administrative_area']['#options'] = array(
  154. '' => t('--'),
  155. 'AG' => 'Agrigento',
  156. 'AL' => 'Alessandria',
  157. 'AN' => 'Ancona',
  158. 'AO' => 'Valle d\'Aosta/Vallée d\'Aoste',
  159. 'AP' => 'Ascoli Piceno',
  160. 'AQ' => 'L\'Aquila',
  161. 'AR' => 'Arezzo',
  162. 'AT' => 'Asti',
  163. 'AV' => 'Avellino',
  164. 'BA' => 'Bari',
  165. 'BG' => 'Bergamo',
  166. 'BI' => 'Biella',
  167. 'BL' => 'Belluno',
  168. 'BN' => 'Benevento',
  169. 'BO' => 'Bologna',
  170. 'BR' => 'Brindisi',
  171. 'BS' => 'Brescia',
  172. 'BT' => 'Barletta-Andria-Trani',
  173. 'BZ' => 'Bolzano/Bozen',
  174. 'CA' => 'Cagliari',
  175. 'CB' => 'Campobasso',
  176. 'CE' => 'Caserta',
  177. 'CH' => 'Chieti',
  178. 'CI' => 'Carbonia-Iglesias',
  179. 'CL' => 'Caltanissetta',
  180. 'CN' => 'Cuneo',
  181. 'CO' => 'Como',
  182. 'CR' => 'Cremona',
  183. 'CS' => 'Cosenza',
  184. 'CT' => 'Catania',
  185. 'CZ' => 'Catanzaro',
  186. 'EN' => 'Enna',
  187. 'FC' => 'Forlì-Cesena',
  188. 'FE' => 'Ferrara',
  189. 'FG' => 'Foggia',
  190. 'FI' => 'Firenze',
  191. 'FM' => 'Fermo',
  192. 'FR' => 'Frosinone',
  193. 'GE' => 'Genova',
  194. 'GO' => 'Gorizia',
  195. 'GR' => 'Grosseto',
  196. 'IM' => 'Imperia',
  197. 'IS' => 'Isernia',
  198. 'KR' => 'Crotone',
  199. 'LC' => 'Lecco',
  200. 'LE' => 'Lecce',
  201. 'LI' => 'Livorno',
  202. 'LO' => 'Lodi',
  203. 'LT' => 'Latina',
  204. 'LU' => 'Lucca',
  205. 'MB' => 'Monza e Brianza',
  206. 'MC' => 'Macerata',
  207. 'ME' => 'Messina',
  208. 'MI' => 'Milano',
  209. 'MN' => 'Mantova',
  210. 'MO' => 'Modena',
  211. 'MS' => 'Massa-Carrara',
  212. 'MT' => 'Matera',
  213. 'NA' => 'Napoli',
  214. 'NO' => 'Novara',
  215. 'NU' => 'Nuoro',
  216. 'OG' => 'Ogliastra',
  217. 'OR' => 'Oristano',
  218. 'OT' => 'Olbia-Tempio',
  219. 'PA' => 'Palermo',
  220. 'PC' => 'Piacenza',
  221. 'PD' => 'Padova',
  222. 'PE' => 'Pescara',
  223. 'PG' => 'Perugia',
  224. 'PI' => 'Pisa',
  225. 'PN' => 'Pordenone',
  226. 'PO' => 'Prato',
  227. 'PR' => 'Parma',
  228. 'PT' => 'Pistoia',
  229. 'PU' => 'Pesaro e Urbino',
  230. 'PV' => 'Pavia',
  231. 'PZ' => 'Potenza',
  232. 'RA' => 'Ravenna',
  233. 'RC' => 'Reggio di Calabria',
  234. 'RE' => 'Reggio nell\'Emilia',
  235. 'RG' => 'Ragusa',
  236. 'RI' => 'Rieti',
  237. 'RM' => 'Roma',
  238. 'RN' => 'Rimini',
  239. 'RO' => 'Rovigo',
  240. 'SA' => 'Salerno',
  241. 'SI' => 'Siena',
  242. 'SO' => 'Sondrio',
  243. 'SP' => 'La Spezia',
  244. 'SR' => 'Siracusa',
  245. 'SS' => 'Sassari',
  246. 'SV' => 'Savona',
  247. 'TA' => 'Taranto',
  248. 'TE' => 'Teramo',
  249. 'TN' => 'Trento',
  250. 'TO' => 'Torino',
  251. 'TP' => 'Trapani',
  252. 'TR' => 'Terni',
  253. 'TS' => 'Trieste',
  254. 'TV' => 'Treviso',
  255. 'UD' => 'Udine',
  256. 'VA' => 'Varese',
  257. 'VB' => 'Verbano-Cusio-Ossola',
  258. 'VC' => 'Vercelli',
  259. 'VE' => 'Venezia',
  260. 'VI' => 'Vicenza',
  261. 'VR' => 'Verona',
  262. 'VS' => 'Medio Campidano',
  263. 'VT' => 'Viterbo',
  264. 'VV' => 'Vibo Valentia',
  265. );
  266. $format['locality_block']['administrative_area']['#title'] = t('Province');
  267. }
  268. else if ($address['country'] == 'BR') {
  269. $format['locality_block']['administrative_area']['#options'] = array(
  270. '' => t('--'),
  271. 'AC' => t('Acre'),
  272. 'AL' => t('Alagoas'),
  273. 'AM' => t('Amazonas'),
  274. 'AP' => t('Amapa'),
  275. 'BA' => t('Bahia'),
  276. 'CE' => t('Ceara'),
  277. 'DF' => t('Distrito Federal'),
  278. 'ES' => t('Espirito Santo'),
  279. 'GO' => t('Goias'),
  280. 'MA' => t('Maranhao'),
  281. 'MG' => t('Minas Gerais'),
  282. 'MS' => t('Mato Grosso do Sul'),
  283. 'MT' => t('Mato Grosso'),
  284. 'PA' => t('Para'),
  285. 'PB' => t('Paraiba'),
  286. 'PE' => t('Pernambuco'),
  287. 'PI' => t('Piaui'),
  288. 'PR' => t('Parana'),
  289. 'RJ' => t('Rio de Janeiro'),
  290. 'RN' => t('Rio Grande do Norte'),
  291. 'RO' => t('Rondonia'),
  292. 'RR' => t('Roraima'),
  293. 'RS' => t('Rio Grande do Sul'),
  294. 'SC' => t('Santa Catarina'),
  295. 'SE' => t('Sergipe'),
  296. 'SP' => t('Sao Paulo'),
  297. 'TO' => t('Tocantins'),
  298. );
  299. }
  300. else if ($address['country'] == 'CA') {
  301. $format['locality_block']['administrative_area']['#options'] = array(
  302. '' => t('--'),
  303. 'AB' => t('Alberta'),
  304. 'BC' => t('British Columbia'),
  305. 'MB' => t('Manitoba'),
  306. 'NB' => t('New Brunswick'),
  307. 'NL' => t('Newfoundland'),
  308. 'NT' => t('Northwest Territories'),
  309. 'NS' => t('Nova Scotia'),
  310. 'NU' => t('Nunavut'),
  311. 'ON' => t('Ontario'),
  312. 'PE' => t('Prince Edward Island'),
  313. 'QC' => t('Quebec'),
  314. 'SK' => t('Saskatchewan'),
  315. 'YT' => t('Yukon Territory'),
  316. );
  317. $format['locality_block']['administrative_area']['#title'] = t('Province');
  318. }
  319. else if ($address['country'] == 'AU') {
  320. $format['locality_block']['administrative_area']['#options'] = array(
  321. '' => t('--'),
  322. 'ACT' => t('Australian Capital Territory'),
  323. 'NSW' => t('New South Wales'),
  324. 'NT' => t('Northern Territory'),
  325. 'QLD' => t('Queensland'),
  326. 'SA' => t('South Australia'),
  327. 'TAS' => t('Tasmania'),
  328. 'VIC' => t('Victoria'),
  329. 'WA' => t('Western Australia'),
  330. );
  331. }
  332. // Those countries tend to put the postal code after the locality.
  333. if (in_array($address['country'], array('AU', 'BD', 'BF', 'BH', 'BM', 'BN', 'BT', 'CA', 'FM', 'GB', 'ID', 'IN', 'JM', 'JO', 'KH', 'LB', 'LS', 'LV', 'MM', 'MN', 'MV', 'MW', 'NG', 'NP', 'NZ', 'PE', 'PK', 'PR', 'PW', 'SA', 'SG', 'SO', 'TH', 'US', 'VI', 'VG', 'VN'))) {
  334. // Take the widget out of the array.
  335. $postal_code_widget = $format['locality_block']['postal_code'];
  336. $postal_code_widget['#prefix'] = ' ';
  337. unset($format['locality_block']['postal_code']);
  338. // Add it back.
  339. $format['locality_block']['postal_code'] = $postal_code_widget;
  340. // Remove the prefix from the first widget of the block.
  341. $element_children = element_children($format['locality_block']);
  342. $first_child = reset($element_children);
  343. unset($format['locality_block'][$first_child]['#prefix']);
  344. }
  345. // GB-specific tweaks
  346. if ($address['country'] == 'GB') {
  347. // Locality
  348. $format['locality_block']['locality'] = array_merge(
  349. $format['locality_block']['locality'],
  350. array(
  351. '#title' => t('Town/City'),
  352. '#weight' => 40,
  353. '#prefix' => '',
  354. '#tag' => 'div',
  355. )
  356. );
  357. // Administrative
  358. $format['locality_block']['administrative_area'] = array_merge(
  359. $format['locality_block']['administrative_area'],
  360. array(
  361. '#title' => t('County'),
  362. '#required' => FALSE,
  363. '#weight' => 50,
  364. '#size' => 30,
  365. '#prefix' => '',
  366. '#tag' => 'div',
  367. )
  368. );
  369. // Postal code
  370. $format['locality_block']['postal_code'] = array_merge(
  371. $format['locality_block']['postal_code'],
  372. array(
  373. '#title' => t('Postcode'),
  374. '#weight' => 60,
  375. '#prefix' => '',
  376. '#tag' => 'div',
  377. )
  378. );
  379. }
  380. if ($context['mode'] == 'form') {
  381. // Provide a wrapper ID for AJAX replacement based on country selection.
  382. if (!isset($format['#wrapper_id'])) {
  383. $format['#wrapper_id'] = drupal_html_id('addressfield-wrapper');
  384. $format['#prefix'] = '<div id="' . $format['#wrapper_id'] . '">';
  385. $format['#suffix'] = '</div>';
  386. }
  387. // Form mode, move the country selector to the top of the form.
  388. $format['country']['#weight'] = -10;
  389. // Limit it to the countries supported by the widget.
  390. if (isset($context['field'])) {
  391. $format['country']['#options'] = _addressfield_country_options_list($context['field'], $context['instance']);
  392. }
  393. // AJAX enable it.
  394. $format['country']['#ajax'] = array(
  395. 'callback' => 'addressfield_standard_widget_refresh',
  396. 'wrapper' => $format['#wrapper_id'],
  397. 'method' => 'replace',
  398. );
  399. $format['country']['#element_validate'] = array('addressfield_standard_country_validate');
  400. // Don't validate any element when the country is changed.
  401. $format['country']['#limit_validation_errors'] = array();
  402. if (isset($context['delta']) && $context['delta'] > 0) {
  403. // On subsequent elements of a field, we make the country field non
  404. // required and add a ' - None - ' option to it, so as to allow the
  405. // user to remove the address by clearing the country field.
  406. $format['country']['#required'] = FALSE;
  407. $format['country']['#empty_value'] = '';
  408. }
  409. }
  410. }