addressfield.module 25 KB

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