address.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. '#render_option_value' => TRUE,
  64. '#required' => TRUE,
  65. '#attributes' => array('class' => array('country')),
  66. '#weight' => 100,
  67. );
  68. // Those countries do not seem to have a relevant postal code.
  69. static $countries_no_postal_code = 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', 'HK', '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');
  70. if (in_array($address['country'], $countries_no_postal_code)) {
  71. unset($format['locality_block']['postal_code']);
  72. // Remove the prefix from the first widget of the block.
  73. $element_children = element_children($format['locality_block']);
  74. $first_child = reset($element_children);
  75. unset($format['locality_block'][$first_child]['#prefix']);
  76. }
  77. // Those countries generally use the administrative area in postal addresses.
  78. static $countries_administrative_area = 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');
  79. if (in_array($address['country'], $countries_administrative_area)) {
  80. $format['locality_block']['administrative_area'] = array(
  81. '#title' => t('State', array(), array('context' => 'addressfield')),
  82. '#size' => 10,
  83. '#required' => TRUE,
  84. '#prefix' => ' ',
  85. '#attributes' => array('class' => array('state')),
  86. );
  87. }
  88. // A few countries have a well-known list of administrative divisions.
  89. if ($address['country'] == 'US') {
  90. $format['locality_block']['administrative_area']['#options'] = array(
  91. '' => t('--'),
  92. 'AL' => t('Alabama'),
  93. 'AK' => t('Alaska'),
  94. 'AZ' => t('Arizona'),
  95. 'AR' => t('Arkansas'),
  96. 'CA' => t('California'),
  97. 'CO' => t('Colorado'),
  98. 'CT' => t('Connecticut'),
  99. 'DE' => t('Delaware'),
  100. 'DC' => t('District Of Columbia'),
  101. 'FL' => t('Florida'),
  102. 'GA' => t('Georgia'),
  103. 'HI' => t('Hawaii'),
  104. 'ID' => t('Idaho'),
  105. 'IL' => t('Illinois'),
  106. 'IN' => t('Indiana'),
  107. 'IA' => t('Iowa'),
  108. 'KS' => t('Kansas'),
  109. 'KY' => t('Kentucky'),
  110. 'LA' => t('Louisiana'),
  111. 'ME' => t('Maine'),
  112. 'MD' => t('Maryland'),
  113. 'MA' => t('Massachusetts'),
  114. 'MI' => t('Michigan'),
  115. 'MN' => t('Minnesota'),
  116. 'MS' => t('Mississippi'),
  117. 'MO' => t('Missouri'),
  118. 'MT' => t('Montana'),
  119. 'NE' => t('Nebraska'),
  120. 'NV' => t('Nevada'),
  121. 'NH' => t('New Hampshire'),
  122. 'NJ' => t('New Jersey'),
  123. 'NM' => t('New Mexico'),
  124. 'NY' => t('New York'),
  125. 'NC' => t('North Carolina'),
  126. 'ND' => t('North Dakota'),
  127. 'OH' => t('Ohio'),
  128. 'OK' => t('Oklahoma'),
  129. 'OR' => t('Oregon'),
  130. 'PA' => t('Pennsylvania'),
  131. 'RI' => t('Rhode Island'),
  132. 'SC' => t('South Carolina'),
  133. 'SD' => t('South Dakota'),
  134. 'TN' => t('Tennessee'),
  135. 'TX' => t('Texas'),
  136. 'UT' => t('Utah'),
  137. 'VT' => t('Vermont'),
  138. 'VA' => t('Virginia'),
  139. 'WA' => t('Washington'),
  140. 'WV' => t('West Virginia'),
  141. 'WI' => t('Wisconsin'),
  142. 'WY' => t('Wyoming'),
  143. ' ' => t('--'),
  144. 'AA' => t('Armed Forces (Americas)'),
  145. 'AE' => t('Armed Forces (Europe, Canada, Middle East, Africa)'),
  146. 'AP' => t('Armed Forces (Pacific)'),
  147. 'AS' => t('American Samoa'),
  148. 'FM' => t('Federated States of Micronesia'),
  149. 'GU' => t('Guam'),
  150. 'MH' => t('Marshall Islands'),
  151. 'MP' => t('Northern Mariana Islands'),
  152. 'PW' => t('Palau'),
  153. 'PR' => t('Puerto Rico'),
  154. 'VI' => t('Virgin Islands'),
  155. );
  156. $format['locality_block']['postal_code']['#title'] = t('ZIP Code');
  157. if ($context['mode'] == 'render') {
  158. $format['locality_block']['locality']['#suffix'] = ',';
  159. }
  160. }
  161. else if ($address['country'] == 'IT') {
  162. $format['locality_block']['administrative_area']['#options'] = array(
  163. '' => t('--'),
  164. 'AG' => t('Agrigento'),
  165. 'AL' => t('Alessandria'),
  166. 'AN' => t('Ancona'),
  167. 'AO' => t("Valle d'Aosta/Vallée d'Aoste"),
  168. 'AP' => t('Ascoli Piceno'),
  169. 'AQ' => t("L'Aquila"),
  170. 'AR' => t('Arezzo'),
  171. 'AT' => t('Asti'),
  172. 'AV' => t('Avellino'),
  173. 'BA' => t('Bari'),
  174. 'BG' => t('Bergamo'),
  175. 'BI' => t('Biella'),
  176. 'BL' => t('Belluno'),
  177. 'BN' => t('Benevento'),
  178. 'BO' => t('Bologna'),
  179. 'BR' => t('Brindisi'),
  180. 'BS' => t('Brescia'),
  181. 'BT' => t('Barletta-Andria-Trani'),
  182. 'BZ' => t('Bolzano/Bozen'),
  183. 'CA' => t('Cagliari'),
  184. 'CB' => t('Campobasso'),
  185. 'CE' => t('Caserta'),
  186. 'CH' => t('Chieti'),
  187. 'CI' => t('Carbonia-Iglesias'),
  188. 'CL' => t('Caltanissetta'),
  189. 'CN' => t('Cuneo'),
  190. 'CO' => t('Como'),
  191. 'CR' => t('Cremona'),
  192. 'CS' => t('Cosenza'),
  193. 'CT' => t('Catania'),
  194. 'CZ' => t('Catanzaro'),
  195. 'EN' => t('Enna'),
  196. 'FC' => t('Forlì-Cesena'),
  197. 'FE' => t('Ferrara'),
  198. 'FG' => t('Foggia'),
  199. 'FI' => t('Firenze'),
  200. 'FM' => t('Fermo'),
  201. 'FR' => t('Frosinone'),
  202. 'GE' => t('Genova'),
  203. 'GO' => t('Gorizia'),
  204. 'GR' => t('Grosseto'),
  205. 'IM' => t('Imperia'),
  206. 'IS' => t('Isernia'),
  207. 'KR' => t('Crotone'),
  208. 'LC' => t('Lecco'),
  209. 'LE' => t('Lecce'),
  210. 'LI' => t('Livorno'),
  211. 'LO' => t('Lodi'),
  212. 'LT' => t('Latina'),
  213. 'LU' => t('Lucca'),
  214. 'MB' => t('Monza e Brianza'),
  215. 'MC' => t('Macerata'),
  216. 'ME' => t('Messina'),
  217. 'MI' => t('Milano'),
  218. 'MN' => t('Mantova'),
  219. 'MO' => t('Modena'),
  220. 'MS' => t('Massa-Carrara'),
  221. 'MT' => t('Matera'),
  222. 'NA' => t('Napoli'),
  223. 'NO' => t('Novara'),
  224. 'NU' => t('Nuoro'),
  225. 'OG' => t('Ogliastra'),
  226. 'OR' => t('Oristano'),
  227. 'OT' => t('Olbia-Tempio'),
  228. 'PA' => t('Palermo'),
  229. 'PC' => t('Piacenza'),
  230. 'PD' => t('Padova'),
  231. 'PE' => t('Pescara'),
  232. 'PG' => t('Perugia'),
  233. 'PI' => t('Pisa'),
  234. 'PN' => t('Pordenone'),
  235. 'PO' => t('Prato'),
  236. 'PR' => t('Parma'),
  237. 'PT' => t('Pistoia'),
  238. 'PU' => t('Pesaro e Urbino'),
  239. 'PV' => t('Pavia'),
  240. 'PZ' => t('Potenza'),
  241. 'RA' => t('Ravenna'),
  242. 'RC' => t('Reggio di Calabria'),
  243. 'RE' => t("Reggio nell'Emilia"),
  244. 'RG' => t('Ragusa'),
  245. 'RI' => t('Rieti'),
  246. 'RM' => t('Roma'),
  247. 'RN' => t('Rimini'),
  248. 'RO' => t('Rovigo'),
  249. 'SA' => t('Salerno'),
  250. 'SI' => t('Siena'),
  251. 'SO' => t('Sondrio'),
  252. 'SP' => t('La Spezia'),
  253. 'SR' => t('Siracusa'),
  254. 'SS' => t('Sassari'),
  255. 'SV' => t('Savona'),
  256. 'TA' => t('Taranto'),
  257. 'TE' => t('Teramo'),
  258. 'TN' => t('Trento'),
  259. 'TO' => t('Torino'),
  260. 'TP' => t('Trapani'),
  261. 'TR' => t('Terni'),
  262. 'TS' => t('Trieste'),
  263. 'TV' => t('Treviso'),
  264. 'UD' => t('Udine'),
  265. 'VA' => t('Varese'),
  266. 'VB' => t('Verbano-Cusio-Ossola'),
  267. 'VC' => t('Vercelli'),
  268. 'VE' => t('Venezia'),
  269. 'VI' => t('Vicenza'),
  270. 'VR' => t('Verona'),
  271. 'VS' => t('Medio Campidano'),
  272. 'VT' => t('Viterbo'),
  273. 'VV' => t('Vibo Valentia'),
  274. );
  275. $format['locality_block']['administrative_area']['#title'] = t('Province');
  276. }
  277. else if ($address['country'] == 'BR') {
  278. $format['locality_block']['administrative_area']['#render_option_value'] = TRUE;
  279. $format['locality_block']['administrative_area']['#options'] = array(
  280. '' => t('--'),
  281. 'AC' => t('Acre'),
  282. 'AL' => t('Alagoas'),
  283. 'AM' => t('Amazonas'),
  284. 'AP' => t('Amapá'),
  285. 'BA' => t('Bahia'),
  286. 'CE' => t('Ceará'),
  287. 'DF' => t('Distrito Federal'),
  288. 'ES' => t('Espírito Santo'),
  289. 'GO' => t('Goiás'),
  290. 'MA' => t('Maranhão'),
  291. 'MG' => t('Minas Gerais'),
  292. 'MS' => t('Mato Grosso do Sul'),
  293. 'MT' => t('Mato Grosso'),
  294. 'PA' => t('Pará'),
  295. 'PB' => t('Paraíba'),
  296. 'PE' => t('Pernambuco'),
  297. 'PI' => t('Piauí'),
  298. 'PR' => t('Paraná'),
  299. 'RJ' => t('Rio de Janeiro'),
  300. 'RN' => t('Rio Grande do Norte'),
  301. 'RO' => t('Rondônia'),
  302. 'RR' => t('Roraima'),
  303. 'RS' => t('Rio Grande do Sul'),
  304. 'SC' => t('Santa Catarina'),
  305. 'SE' => t('Sergipe'),
  306. 'SP' => t('São Paulo'),
  307. 'TO' => t('Tocantins'),
  308. );
  309. }
  310. else if ($address['country'] == 'CA') {
  311. $format['locality_block']['administrative_area']['#options'] = array(
  312. '' => t('--'),
  313. 'AB' => t('Alberta'),
  314. 'BC' => t('British Columbia'),
  315. 'MB' => t('Manitoba'),
  316. 'NB' => t('New Brunswick'),
  317. 'NL' => t('Newfoundland and Labrador'),
  318. 'NT' => t('Northwest Territories'),
  319. 'NS' => t('Nova Scotia'),
  320. 'NU' => t('Nunavut'),
  321. 'ON' => t('Ontario'),
  322. 'PE' => t('Prince Edward Island'),
  323. 'QC' => t('Quebec'),
  324. 'SK' => t('Saskatchewan'),
  325. 'YT' => t('Yukon Territory'),
  326. );
  327. $format['locality_block']['administrative_area']['#title'] = t('Province');
  328. if ($context['mode'] == 'render') {
  329. $format['locality_block']['locality']['#suffix'] = ',';
  330. }
  331. }
  332. else if ($address['country'] == 'AU') {
  333. $format['locality_block']['administrative_area']['#options'] = array(
  334. '' => t('--'),
  335. 'ACT' => t('Australian Capital Territory'),
  336. 'NSW' => t('New South Wales'),
  337. 'NT' => t('Northern Territory'),
  338. 'QLD' => t('Queensland'),
  339. 'SA' => t('South Australia'),
  340. 'TAS' => t('Tasmania'),
  341. 'VIC' => t('Victoria'),
  342. 'WA' => t('Western Australia'),
  343. );
  344. }
  345. else if ($address['country'] == 'NZ') {
  346. $format['locality_block']['locality']['#title'] = ('Town/City');
  347. $format['locality_block']['postal_code']['#title'] = t('Postcode');
  348. $format['locality_block']['administrative_area']['#render_option_value'] = TRUE;
  349. $format['locality_block']['administrative_area']['#title'] = t('Region');
  350. $format['locality_block']['administrative_area']['#required'] = FALSE;
  351. $format['locality_block']['administrative_area']['#options'] = array(
  352. '' => t('--'),
  353. 'AUK' => t('Auckland'),
  354. 'BOP' => t('Bay of Plenty'),
  355. 'CAN' => t('Canterbury'),
  356. 'HKB' => t("Hawke's Bay"),
  357. 'MWT' => t('Manawatu-Wanganui'),
  358. 'NTL' => t('Northland'),
  359. 'OTA' => t('Otago'),
  360. 'STL' => t('Southland'),
  361. 'TKI' => t('Taranaki'),
  362. 'WKO' => t('Waikato'),
  363. 'WGN' => t('Wellington'),
  364. 'WTC' => t('West Coast'),
  365. 'GIS' => t('Gisborne District'),
  366. 'MBH' => t('Marlborough District'),
  367. 'NSN' => t('Nelson City'),
  368. 'TAS' => t('Tasman District'),
  369. 'CIT' => t('Chatham Islands Territory'),
  370. );
  371. }
  372. // Those countries tend to put the postal code after the locality.
  373. static $countries_postal_code_after_locality = 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');
  374. if (in_array($address['country'], $countries_postal_code_after_locality)) {
  375. // Take the widget out of the array.
  376. $postal_code_widget = $format['locality_block']['postal_code'];
  377. $postal_code_widget['#prefix'] = ' ';
  378. unset($format['locality_block']['postal_code']);
  379. // Add it back.
  380. $format['locality_block']['postal_code'] = $postal_code_widget;
  381. // Remove the prefix from the first widget of the block.
  382. $element_children = element_children($format['locality_block']);
  383. $first_child = reset($element_children);
  384. unset($format['locality_block'][$first_child]['#prefix']);
  385. }
  386. // GB-specific tweaks
  387. if ($address['country'] == 'GB') {
  388. // Locality
  389. $format['locality_block']['locality'] = array_merge(
  390. $format['locality_block']['locality'],
  391. array(
  392. '#title' => t('Town/City'),
  393. '#weight' => 40,
  394. '#prefix' => '',
  395. '#tag' => 'div',
  396. )
  397. );
  398. // Administrative
  399. $format['locality_block']['administrative_area'] = array_merge(
  400. $format['locality_block']['administrative_area'],
  401. array(
  402. '#title' => t('County'),
  403. '#required' => FALSE,
  404. '#weight' => 50,
  405. '#size' => 30,
  406. '#prefix' => '',
  407. '#tag' => 'div',
  408. )
  409. );
  410. // Postal code
  411. $format['locality_block']['postal_code'] = array_merge(
  412. $format['locality_block']['postal_code'],
  413. array(
  414. '#title' => t('Postcode'),
  415. '#weight' => 60,
  416. '#prefix' => '',
  417. '#tag' => 'div',
  418. )
  419. );
  420. }
  421. if ($context['mode'] == 'form') {
  422. // Provide a wrapper ID for AJAX replacement based on country selection.
  423. if (!isset($format['#wrapper_id'])) {
  424. $format['#wrapper_id'] = drupal_html_id('addressfield-wrapper');
  425. $format['#prefix'] = '<div id="' . $format['#wrapper_id'] . '">';
  426. $format['#suffix'] = '</div>';
  427. }
  428. // Form mode, move the country selector to the top of the form.
  429. $format['country']['#weight'] = -10;
  430. // Limit it to the countries supported by the widget.
  431. if (isset($context['field'])) {
  432. $format['country']['#options'] = _addressfield_country_options_list($context['field'], $context['instance']);
  433. }
  434. // AJAX enable it.
  435. $format['country']['#ajax'] = array(
  436. 'callback' => 'addressfield_standard_widget_refresh',
  437. 'wrapper' => $format['#wrapper_id'],
  438. );
  439. $format['country']['#element_validate'] = array('addressfield_standard_country_validate');
  440. // Don't validate any element when the country is changed.
  441. $format['country']['#limit_validation_errors'] = array();
  442. if (isset($context['delta']) && $context['delta'] > 0) {
  443. // On subsequent elements of a field, we make the country field non
  444. // required and add a ' - None - ' option to it, so as to allow the
  445. // user to remove the address by clearing the country field.
  446. $format['country']['#required'] = FALSE;
  447. $format['country']['#empty_value'] = '';
  448. }
  449. }
  450. }