addressfield.module 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. <?php
  2. /**
  3. * @file
  4. * Defines a field for attaching country-specific addresses to entities.
  5. */
  6. /**
  7. * Implements hook_ctools_plugin_directory().
  8. */
  9. function addressfield_ctools_plugin_directory($module, $plugin) {
  10. if ($module == 'addressfield') {
  11. return 'plugins/' . $plugin;
  12. }
  13. }
  14. /**
  15. * Implements hook_ctools_plugin_type().
  16. */
  17. function addressfield_ctools_plugin_type() {
  18. $plugins['format'] = array(
  19. 'load themes' => TRUE,
  20. );
  21. return $plugins;
  22. }
  23. /**
  24. * Implements hook_views_api().
  25. */
  26. function addressfield_views_api() {
  27. return array(
  28. 'api' => 3,
  29. 'path' => drupal_get_path('module', 'addressfield') . '/views',
  30. );
  31. }
  32. /**
  33. * Get the list of format plugins.
  34. */
  35. function addressfield_format_plugins() {
  36. ctools_include('plugins');
  37. $plugins = ctools_get_plugins('addressfield', 'format');
  38. uasort($plugins, 'ctools_plugin_sort');
  39. return $plugins;
  40. }
  41. /**
  42. * Get the list of format plugins in a format suitable for #options.
  43. */
  44. function addressfield_format_plugins_options() {
  45. $options = array();
  46. foreach (addressfield_format_plugins() as $widget => $info) {
  47. $options[$widget] = check_plain($info['title']);
  48. }
  49. return $options;
  50. }
  51. /**
  52. * @defgroup addressfield_format Address format API
  53. * @{
  54. * API for generating address forms and display formats.
  55. *
  56. * Addresses forms and display formats are collaboratively generated by one or
  57. * more format handler plugins. An address with a name and a company, for example,
  58. * will be generated by three handlers:
  59. * - 'address' that will generate the country, locality, street blocks
  60. * - 'organisation' that will add the organisation block to the address
  61. * - 'name-full' that will add a first name and last name block to the address
  62. *
  63. * A format handler is a CTools plugin of type 'addressfield' / 'format'. Each
  64. * handler is passed the format in turn, and can add to or modify the format.
  65. *
  66. * The format itself is a renderable array stub. This stub will be transformed
  67. * into either a Form API array suitable for use as part of a form or into a
  68. * renderable array suitable for use with drupal_render(). The following
  69. * modifications are done:
  70. * - when rendering as a form, every element which name (its key in the array)
  71. * is a valid addressfield column (see addressfield_field_schema()), will
  72. * be transformed into a form element, either using a type explicitly
  73. * defined in '#widget_type' or using 'select' if '#options' is set or
  74. * 'textfield' if it is not. In addition, the '#default_value' of every
  75. * field will be populated from the address being edited.
  76. * - when rendering as a formatter, every element which name (its key in the array)
  77. * is a valid addressfield column (see addressfield_field_schema()), will
  78. * be transformed into a renderable element, either using a type explicitly
  79. * defined in '#render_type' or else using 'addressfield_container'. When
  80. * the type is 'addressfield_container' the element will be rendered as
  81. * an HTML element set by '#tag' (default: span).
  82. */
  83. /**
  84. * Generate a format for a given address.
  85. *
  86. * @param $address
  87. * The address format being generated.
  88. * @param $handlers
  89. * The format handlers to use to generate the format.
  90. * @param $context
  91. * An array of context arguments:
  92. * - 'mode': can be either 'form' or 'render'
  93. * - (optional) 'field': when generated for a field, the field
  94. * - (optional) 'instance': when generated for a field, the field instance
  95. * - (optional) 'langcode': when generated for a field, the langcode
  96. * this field is being rendered in.
  97. * - (optional) 'delta': when generated for a field, the delta of the
  98. * currently handled address.
  99. *
  100. * @return
  101. * A rendered array suitable for use as part of a form (if 'mode' is 'form'),
  102. * or suitable to pass to drupal_render().
  103. */
  104. function addressfield_generate($address, array $handlers, array $context = array()) {
  105. ctools_include('plugins');
  106. $format = array();
  107. $format['#handlers'] = $handlers;
  108. foreach ($format['#handlers'] as $handler) {
  109. if ($callback = ctools_plugin_load_function('addressfield', 'format', $handler, 'format callback')) {
  110. $callback($format, $address, $context);
  111. }
  112. }
  113. // Store the address in the format, for processing.
  114. $format['#address'] = $address;
  115. // Post-process the format stub, depending on the rendering mode.
  116. if ($context['mode'] == 'form') {
  117. $format['#addressfield'] = TRUE;
  118. $format['#process'][] = 'addressfield_process_format_form';
  119. $format['#required'] = FALSE;
  120. }
  121. else {
  122. $format['#pre_render'][] = 'addressfield_render_address';
  123. }
  124. return $format;
  125. }
  126. /**
  127. * Generate a full-fledged form from a format snippet, as returned by addressfield_formats().
  128. */
  129. function addressfield_process_format_form($format, &$form_state, $complete_form) {
  130. // Make sure to load all the plugins that participated in this format.
  131. ctools_include('plugins');
  132. foreach ($format['#handlers'] as $handler) {
  133. ctools_plugin_load_function('addressfield', 'format', $handler, 'format callback');
  134. }
  135. _addressfield_process_format_form($format, $format['#address'], $format['#required']);
  136. return $format;
  137. }
  138. function _addressfield_process_format_form(&$format, $address, $required) {
  139. foreach (element_children($format) as $key) {
  140. $child = &$format[$key];
  141. // Automatically expand elements that matches one of the field of the
  142. // address structure.
  143. if (in_array($key, array('name_line', 'first_name', 'last_name', 'organisation_name', 'country', 'administrative_area', 'sub_administrative_area', 'locality', 'dependent_locality', 'postal_code', 'thoroughfare', 'premise', 'sub_premise'))) {
  144. // Set the type.
  145. if (isset($child['#widget_type'])) {
  146. $child['#type'] = $child['#widget_type'];
  147. }
  148. else {
  149. if (isset($child['#options'])) {
  150. $child['#type'] = 'select';
  151. $child['#size'] = 0;
  152. }
  153. else {
  154. $child['#type'] = 'textfield';
  155. }
  156. }
  157. if (!$required) {
  158. unset($child['#required']);
  159. }
  160. $child['#default_value'] = $address[$key];
  161. }
  162. // Recurse through the child.
  163. _addressfield_process_format_form($child, $address, $required);
  164. }
  165. }
  166. /**
  167. * Render an address in a given format.
  168. */
  169. function addressfield_render_address($format) {
  170. _addressfield_render_address($format, $format['#address']);
  171. return $format;
  172. }
  173. function _addressfield_render_address(&$format, $address) {
  174. foreach (element_children($format) as $key) {
  175. $child = &$format[$key];
  176. // Automatically expand elements that matches one of the field of the
  177. // address structure.
  178. if (in_array($key, array('name_line', 'first_name', 'last_name', 'organisation_name', 'country', 'administrative_area', 'sub_administrative_area', 'locality', 'dependent_locality', 'postal_code', 'thoroughfare', 'premise', 'sub_premise'))) {
  179. if (isset($child['#render_type'])) {
  180. $child['#type'] = $child['#render_type'];
  181. }
  182. else {
  183. $child['#type'] = 'addressfield_container';
  184. if (!isset($child['#tag'])) {
  185. $child['#tag'] = 'span';
  186. }
  187. }
  188. if (isset($child['#options'])) {
  189. // Expand options if necessary.
  190. $child['#children'] = isset($child['#options'][$address[$key]]) ? check_plain($child['#options'][$address[$key]]) : '';
  191. }
  192. else {
  193. $child['#children'] = check_plain($address[$key]);
  194. }
  195. // Skip empty elements.
  196. if ((string) $child['#children'] === '') {
  197. $child['#access'] = FALSE;
  198. }
  199. // Add #prefix and #field_suffix to the prefixes and suffixes.
  200. if (isset($child['#prefix'])) {
  201. $child['#prefix'] = (isset($child['#prefix']) ? $child['#prefix'] : '') . $child['#prefix'];
  202. }
  203. if (isset($child['#field_suffix'])) {
  204. $child['#suffix'] = (isset($child['#suffix']) ? $child['#suffix'] : '') . $child['#field_suffix'];
  205. }
  206. }
  207. // Recurse through the child.
  208. _addressfield_render_address($child, $address);
  209. }
  210. }
  211. /**
  212. * @} End of "ingroup addressfield_format"
  213. */
  214. /**
  215. * Implementation of hook_theme().
  216. */
  217. function addressfield_theme() {
  218. $hooks['addressfield_container'] = array(
  219. 'render element' => 'element',
  220. );
  221. return $hooks;
  222. }
  223. /**
  224. * Render a container for a set of address fields.
  225. */
  226. function theme_addressfield_container($variables) {
  227. $element = $variables['element'];
  228. $element['#children'] = trim($element['#children']);
  229. if (strlen($element['#children']) > 0) {
  230. $output = '<' . $element['#tag'] . drupal_attributes($element['#attributes']) . '>';
  231. $output .= $element['#children'];
  232. $output .= '</' . $element['#tag'] . ">";
  233. return $output;
  234. }
  235. else {
  236. return '';
  237. }
  238. }
  239. /**
  240. * Implementation of hook_element_info().
  241. */
  242. function addressfield_element_info() {
  243. $types['addressfield_container'] = array(
  244. '#theme_wrappers' => array('addressfield_container'),
  245. '#process' => array('addressfield_widget_process'),
  246. '#attributes' => array(),
  247. '#tag' => 'div',
  248. );
  249. return $types;
  250. }
  251. /**
  252. * Form API process function: set the #parents of the children of this element so they appear at the same level as the parent.
  253. */
  254. function addressfield_widget_process($element) {
  255. foreach (element_children($element) as $key) {
  256. $element[$key]['#parents'] = $element['#parents'];
  257. $element[$key]['#parents'][count($element[$key]['#parents']) - 1] = $key;
  258. }
  259. return $element;
  260. }
  261. /**
  262. * Implements hook_field_info()
  263. */
  264. function addressfield_field_info() {
  265. $fields = array();
  266. $fields['addressfield'] = array(
  267. 'label' => t('Postal address'),
  268. 'description' => t('A field type used for storing postal addresses according the xNAL standard.'),
  269. 'settings' => array(),
  270. 'instance_settings' => array(),
  271. 'default_widget' => 'addressfield_standard',
  272. 'default_formatter' => 'addressfield_default',
  273. 'property_type' => 'addressfield',
  274. 'property_callbacks' => array('addressfield_property_info_callback'),
  275. );
  276. return $fields;
  277. }
  278. /**
  279. * Returns an array of default values for the addressfield form elements.
  280. */
  281. function addressfield_default_values($available_countries = NULL) {
  282. if (!isset($available_countries)) {
  283. $available_countries = _addressfield_country_options_list();
  284. }
  285. // Use the default country of the site if possible.
  286. $default_country = variable_get('site_default_country', NULL);
  287. // If the default country is undefined or not in the list of available countries,
  288. // just fallback to the first country in the list.
  289. if (!$default_country || !isset($available_countries[$default_country])) {
  290. $default_country = key($available_countries);
  291. }
  292. return array(
  293. 'country' => $default_country,
  294. 'name_line' => '',
  295. 'first_name' => '',
  296. 'last_name' => '',
  297. 'organisation_name' => '',
  298. 'administrative_area' => '',
  299. 'sub_administrative_area' => '',
  300. 'locality' => '',
  301. 'dependent_locality' => '',
  302. 'postal_code' => '',
  303. 'thoroughfare' => '',
  304. 'premise' => '',
  305. 'sub_premise' => '',
  306. 'data' => '',
  307. );
  308. }
  309. /**
  310. * Implements hook_field_is_empty().
  311. */
  312. function addressfield_field_is_empty($item, $field) {
  313. // Every address field must have at least a country value or it is considered
  314. // empty, even if it has name information.
  315. return empty($item['country']);
  316. }
  317. /**
  318. * Implements hook_field_widget_info()
  319. */
  320. function addressfield_field_widget_info() {
  321. $widgets = array();
  322. $widgets['addressfield_standard'] = array(
  323. 'label' => t('Dynamic address form'),
  324. 'field types' => array('addressfield'),
  325. 'settings' => array(
  326. 'available_countries' => array(),
  327. 'format_handlers' => array('address'),
  328. ),
  329. );
  330. return $widgets;
  331. }
  332. /**
  333. * Implements hook_field_widget_settings_form()
  334. */
  335. function addressfield_field_widget_settings_form($field, $instance) {
  336. $widget = $instance['widget'];
  337. $defaults = field_info_widget_settings($widget['type']);
  338. $settings = array_merge($defaults, $widget['settings']);
  339. if ($widget['type'] == 'addressfield_standard') {
  340. $form['available_countries'] = array(
  341. '#type' => 'select',
  342. '#multiple' => TRUE,
  343. '#title' => t('Available countries'),
  344. '#description' => t('If no countries are selected, all countries will be available.'),
  345. '#options' => _addressfield_country_options_list(),
  346. '#default_value' => $settings['available_countries'],
  347. );
  348. $form['format_handlers'] = array(
  349. '#type' => 'checkboxes',
  350. '#title' => t('Format handlers'),
  351. '#options' => addressfield_format_plugins_options(),
  352. '#default_value' => $settings['format_handlers'],
  353. );
  354. }
  355. return $form;
  356. }
  357. /**
  358. * Implements hook_field_widget_form()
  359. */
  360. function addressfield_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  361. $settings = $instance['widget']['settings'];
  362. // Generate a specific key used to identify this element to restore a default
  363. // value upon AJAX submission regardless of where this element is in the
  364. // $form array.
  365. $element_key = implode('|', array($element['#entity_type'], $element['#bundle'], $element['#field_name'], $element['#language'], $element['#delta']));
  366. // Store the key in the element array as a value so it can be easily retrieved
  367. // in context in the $form_state['values'] array in the element validator.
  368. $element['element_key'] = array(
  369. '#type' => 'value',
  370. '#value' => $element_key,
  371. );
  372. // Get the default address used to build the widget form elements, looking
  373. // first in the form state, then in the stored value for the field, and then
  374. // in the default values of the instance.
  375. $address = array();
  376. if (!empty($form_state['addressfield'][$element_key])) {
  377. // Use the value from the form_state if available.
  378. $address = $form_state['addressfield'][$element_key];
  379. }
  380. elseif (!empty($items[$delta]['country'])) {
  381. // Else use the saved value for the field.
  382. $address = $items[$delta];
  383. }
  384. else {
  385. // Otherwise use the instance default.
  386. $address = (array) $instance['default_value'][0];
  387. }
  388. // Merge in default values to provide a value for every expected array key.
  389. $countries = _addressfield_country_options_list($field, $instance);
  390. $address += addressfield_default_values($countries);
  391. // Add the form elements for the standard widget, which includes a country
  392. // select list at the top that reloads the available address elements when the
  393. // country is changed.
  394. if ($instance['widget']['type'] == 'addressfield_standard') {
  395. // Wrap everything in a fieldset. This is not the best looking element,
  396. // but it's the only wrapper available in Drupal we can properly use
  397. // in that context, and it is overridable if necessary.
  398. $element['#type'] = 'fieldset';
  399. // Generate the address form.
  400. $context = array(
  401. 'mode' => 'form',
  402. 'field' => $field,
  403. 'instance' => $instance,
  404. 'langcode' => $langcode,
  405. 'delta' => $delta,
  406. );
  407. $element += addressfield_generate($address, $settings['format_handlers'], $context);
  408. // Mark the form element as required if necessary.
  409. $element['#required'] = $delta == 0 && $instance['required'];
  410. }
  411. return $element;
  412. }
  413. /**
  414. * Element validate callback: rebuilds the form on country change and stores the current address value in the $form_state for retrieval on rebuild.
  415. */
  416. function addressfield_standard_country_validate($element, &$form_state) {
  417. // If the country was changed, rebuild the form.
  418. if ($element['#default_value'] != $element['#value']) {
  419. $form_state['rebuild'] = TRUE;
  420. }
  421. $parents = $element['#parents'];
  422. array_pop($parents);
  423. // Search through the form values to find the current address.
  424. $address = drupal_array_get_nested_value($form_state['values'], $parents);
  425. // Store the present address values in the form state for retrieval by the
  426. // widget form regardless of where the widget sits in the $form array.
  427. $form_state['addressfield'][$address['element_key']] = array_diff_key($address, array('element_key' => ''));
  428. }
  429. /**
  430. * Ajax callback in response to a change of country in an address field.
  431. *
  432. * The only thing we have to do is to find the proper element to render.
  433. */
  434. function addressfield_standard_widget_refresh($form, $form_state) {
  435. // The target element is one element below the triggering country selector.
  436. $array_parents = $form_state['triggering_element']['#array_parents'];
  437. array_pop($array_parents);
  438. // Iterate over the form parents to find the element.
  439. $element = $form;
  440. foreach ($array_parents as $name) {
  441. $element = &$element[$name];
  442. if (!empty($element['#addressfield'])) {
  443. break;
  444. }
  445. }
  446. // Return the address block, but remove the '_weight' element inserted
  447. // by the field API.
  448. unset($element['_weight']);
  449. return $element;
  450. }
  451. /**
  452. * Implements hook_field_formatter_info().
  453. */
  454. function addressfield_field_formatter_info() {
  455. return array(
  456. 'addressfield_default' => array(
  457. 'label' => t('Default'),
  458. 'field types' => array('addressfield'),
  459. 'settings' => array(
  460. 'use_widget_handlers' => 1,
  461. 'format_handlers' => array('address'),
  462. ),
  463. ),
  464. );
  465. }
  466. /**
  467. * Implements hook_field_formatter_settings_form().
  468. */
  469. function addressfield_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  470. $display = $instance['display'][$view_mode];
  471. $settings = $display['settings'];
  472. $element['use_widget_handlers'] = array(
  473. '#type' => 'checkbox',
  474. '#title' => t('Use the same configuration as the widget'),
  475. '#default_value' => !empty($settings['use_widget_handlers']),
  476. );
  477. $element['format_handlers'] = array(
  478. '#type' => 'checkboxes',
  479. '#title' => t('Format handlers'),
  480. '#options' => addressfield_format_plugins_options(),
  481. '#default_value' => $settings['format_handlers'],
  482. '#process' => array('form_process_checkboxes', '_addressfield_field_formatter_settings_form_process_add_state'),
  483. '#element_validate' => array('_addressfield_field_formatter_settings_form_validate')
  484. );
  485. return $element;
  486. }
  487. /**
  488. * Helper function: set the proper #states to the use widget handlers checkbox.
  489. */
  490. function _addressfield_field_formatter_settings_form_process_add_state($element, $form_state) {
  491. // Build a #parents based on the current checkbox.
  492. $target_parents = array_slice($element['#parents'], 0, -1);
  493. $target_parents[] = 'use_widget_handlers';
  494. $target_parents = array_shift($target_parents) . ($target_parents ? '[' . implode('][', $target_parents) . ']' : '');
  495. $element['#states']['visible'] = array(
  496. ':input[name="' . $target_parents . '"]' => array('checked' => FALSE),
  497. );
  498. return $element;
  499. }
  500. /**
  501. * Helper function: filter the results of the checkboxes form element.
  502. */
  503. function _addressfield_field_formatter_settings_form_validate($element, &$element_state) {
  504. form_set_value($element, array_filter($element['#value']), $element_state);
  505. }
  506. /**
  507. * Implements hook_field_formatter_settings_summary().
  508. */
  509. function addressfield_field_formatter_settings_summary($field, $instance, $view_mode) {
  510. $display = $instance['display'][$view_mode];
  511. $settings = $display['settings'];
  512. $summary = '';
  513. if ($settings['use_widget_handlers']) {
  514. return t('Use widget configuration');
  515. }
  516. else {
  517. $summary = array();
  518. $plugins = addressfield_format_plugins();
  519. foreach ($settings['format_handlers'] as $handler) {
  520. $summary[] = $plugins[$handler]['title'];
  521. }
  522. return $summary ? implode(', ', $summary) : t('No handler');
  523. }
  524. }
  525. /**
  526. * Implements hook_field_formatter_view().
  527. */
  528. function addressfield_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  529. $settings = $display['settings'];
  530. $element = array();
  531. switch ($display['type']) {
  532. case 'addressfield_default':
  533. if (!empty($settings['use_widget_handlers'])) {
  534. $handlers = $instance['widget']['settings']['format_handlers'];
  535. }
  536. else {
  537. $handlers = $settings['format_handlers'];
  538. }
  539. foreach ($items as $delta => $address) {
  540. // Generate the address format.
  541. $context = array(
  542. 'mode' => 'render',
  543. 'field' => $field,
  544. 'instance' => $instance,
  545. 'langcode' => $langcode,
  546. 'delta' => $delta,
  547. );
  548. $element[$delta] = addressfield_generate($address, $handlers, $context);
  549. }
  550. break;
  551. }
  552. return $element;
  553. }
  554. /**
  555. * Callback to alter the property info of address fields.
  556. *
  557. * @see addressfield_field_info().
  558. */
  559. function addressfield_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
  560. $name = $field['field_name'];
  561. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
  562. $property['type'] = ($field['cardinality'] != 1) ? 'list<addressfield>' : 'addressfield';
  563. $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  564. $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  565. $property['auto creation'] = 'addressfield_default_values';
  566. $property['property info'] = addressfield_data_property_info();
  567. unset($property['query callback']);
  568. }
  569. /**
  570. * Defines info for the properties of the address field data structure.
  571. */
  572. function addressfield_data_property_info($name = NULL) {
  573. // Build an array of basic property information for the address field.
  574. $properties = array(
  575. 'country' => array(
  576. 'label' => t('Country'),
  577. 'options list' => '_addressfield_country_options_list',
  578. ),
  579. 'name_line' => array(
  580. 'label' => t('Full name'),
  581. ),
  582. 'first_name' => array(
  583. 'label' => t('First name'),
  584. ),
  585. 'last_name' => array(
  586. 'label' => t('Last name'),
  587. ),
  588. 'organisation_name' => array(
  589. 'label' => t('Company'),
  590. ),
  591. 'administrative_area' => array(
  592. 'label' => t('Administrative area (i.e. State / Province)'),
  593. ),
  594. 'sub_administrative_area' => array(
  595. 'label' => t('Sub administrative area'),
  596. ),
  597. 'locality' => array(
  598. 'label' => t('Locality (i.e. City)'),
  599. ),
  600. 'dependent_locality' => array(
  601. 'label' => t('Dependent locality'),
  602. ),
  603. 'postal_code' => array(
  604. 'label' => t('Postal code'),
  605. ),
  606. 'thoroughfare' => array(
  607. 'label' => t('Thoroughfare (i.e. Street address)'),
  608. ),
  609. 'premise' => array(
  610. 'label' => t('Premise (i.e. Apartment / Suite number)'),
  611. ),
  612. 'name_line' => array(
  613. 'label' => t('Sub premise'),
  614. ),
  615. );
  616. // Add the default values for each of the address field properties.
  617. foreach ($properties as $key => &$value) {
  618. $value += array(
  619. 'description' => !empty($name) ? t('!label of field %name', array('!label' => $value['label'], '%name' => $name)) : '',
  620. 'type' => 'text',
  621. 'getter callback' => 'entity_property_verbatim_get',
  622. 'setter callback' => 'entity_property_verbatim_set',
  623. );
  624. }
  625. return $properties;
  626. }
  627. /**
  628. * Wraps country_get_list() for use as an Entity API options list.
  629. */
  630. function _addressfield_country_options_list($field = NULL, $instance = NULL) {
  631. // Necessary for country_get_list().
  632. require_once DRUPAL_ROOT . '/includes/locale.inc';
  633. $countries = country_get_list();
  634. if (isset($field)) {
  635. // If the instance is not specified, loop against all the instances of the field.
  636. if (!isset($instance)) {
  637. $instances = array();
  638. foreach ($field['bundles'] as $entity_type => $bundles) {
  639. foreach ($bundles as $bundle_name) {
  640. $instances[] = field_info_instance($entity_type, $field['field_name'], $bundle_name);
  641. }
  642. }
  643. }
  644. else {
  645. $instances = array($instance);
  646. }
  647. foreach ($instances as $instance) {
  648. if (!empty($instance['widget']['settings']['available_countries'])) {
  649. $countries = array_intersect_key($countries, $instance['widget']['settings']['available_countries']);
  650. }
  651. else {
  652. // This instance allow all the countries.
  653. $countries = country_get_list();
  654. break;
  655. }
  656. }
  657. }
  658. return $countries;
  659. }