addressfield.install 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * Implements hook_field_schema()
  4. */
  5. function addressfield_field_schema() {
  6. $columns = array(
  7. 'country' => array(
  8. 'description' => 'Two letter ISO country code of this address.',
  9. 'type' => 'varchar',
  10. 'length' => 2,
  11. 'not null' => FALSE,
  12. 'default' => '',
  13. ),
  14. 'administrative_area' => array(
  15. 'description' => 'The administrative area of this address. (i.e. State/Province)',
  16. 'type' => 'varchar',
  17. 'length' => 255,
  18. 'default' => '',
  19. 'not null' => FALSE,
  20. ),
  21. 'sub_administrative_area' => array(
  22. 'description' => 'The sub administrative area of this address.',
  23. 'type' => 'varchar',
  24. 'length' => 255,
  25. 'default' => '',
  26. 'not null' => FALSE,
  27. ),
  28. 'locality' => array(
  29. 'description' => 'The locality of this address. (i.e. City)',
  30. 'type' => 'varchar',
  31. 'length' => 255,
  32. 'default' => '',
  33. 'not null' => FALSE,
  34. ),
  35. 'dependent_locality' => array(
  36. 'description' => 'The dependent locality of this address.',
  37. 'type' => 'varchar',
  38. 'length' => 255,
  39. 'default' => '',
  40. 'not null' => FALSE,
  41. ),
  42. 'postal_code' => array(
  43. 'description' => 'The postal code of this address.',
  44. 'type' => 'varchar',
  45. 'length' => 255,
  46. 'default' => '',
  47. 'not null' => FALSE,
  48. ),
  49. 'thoroughfare' => array(
  50. 'description' => 'The thoroughfare of this address. (i.e. Street address)',
  51. 'type' => 'varchar',
  52. 'length' => 255,
  53. 'default' => '',
  54. 'not null' => FALSE,
  55. ),
  56. 'premise' => array(
  57. 'description' => 'The premise of this address. (i.e. Apartment / Suite number)',
  58. 'type' => 'varchar',
  59. 'length' => 255,
  60. 'default' => '',
  61. 'not null' => FALSE,
  62. ),
  63. 'sub_premise' => array(
  64. 'description' => 'The sub_premise of this address.',
  65. 'type' => 'varchar',
  66. 'length' => 255,
  67. 'default' => '',
  68. 'not null' => FALSE,
  69. ),
  70. 'organisation_name' => array(
  71. 'description' => 'Contents of a primary OrganisationName element in the xNL XML.',
  72. 'type' => 'varchar',
  73. 'length' => 255,
  74. 'not null' => FALSE,
  75. 'default' => '',
  76. ),
  77. 'name_line' => array(
  78. 'description' => 'Contents of a primary NameLine element in the xNL XML.',
  79. 'type' => 'varchar',
  80. 'length' => 255,
  81. 'not null' => FALSE,
  82. 'default' => '',
  83. ),
  84. 'first_name' => array(
  85. 'description' => 'Contents of the FirstName element of a primary PersonName element in the xNL XML.',
  86. 'type' => 'varchar',
  87. 'length' => 255,
  88. 'not null' => FALSE,
  89. 'default' => '',
  90. ),
  91. 'last_name' => array(
  92. 'description' => 'Contents of the LastName element of a primary PersonName element in the xNL XML.',
  93. 'type' => 'varchar',
  94. 'length' => 255,
  95. 'not null' => FALSE,
  96. 'default' => '',
  97. ),
  98. 'data' => array(
  99. 'description' => 'Additional data for this address.',
  100. 'type' => 'text',
  101. 'size' => 'big',
  102. 'not null' => FALSE,
  103. 'serialize' => TRUE,
  104. ),
  105. );
  106. return array(
  107. 'columns' => $columns,
  108. // TODO Add indexes.
  109. );
  110. }
  111. /**
  112. * Update the field configuration to the new plugin structure.
  113. */
  114. function addressfield_update_7000() {
  115. // Enable ctools.
  116. if (!module_enable(array('ctools'))) {
  117. throw new Exception('This version of addressfield requires ctools, but it could not be enabled.');
  118. }
  119. // Get the list of fields of type 'addressfield'.
  120. $address_fields = array();
  121. foreach (field_info_fields() as $field_name => $field_info) {
  122. if ($field_info['type'] == 'addressfield') {
  123. $address_fields[$field_name] = $field_name;
  124. }
  125. }
  126. foreach (field_info_instances() as $entity_type => $bundles) {
  127. foreach ($bundles as $bundle_name => $instances) {
  128. foreach (array_intersect_key($instances, $address_fields) as $field_name => $instance) {
  129. $widget_settings = &$instance['widget']['settings'];
  130. if ($instance['widget']['type'] == 'addressfield_standard') {
  131. // Default to use the country-based address widget.
  132. $format_handlers = array('address');
  133. // Map the old 'name_format' setting to the name and organization widgets.
  134. if (in_array($widget_settings['name_format'], array('name_line_organisation', 'first_last_organisation'))) {
  135. $format_handlers[] = 'organisation';
  136. }
  137. if (in_array($widget_settings['name_format'], array('name_line', 'name_line_organisation'))) {
  138. $format_handlers[] = 'name-oneline';
  139. }
  140. else {
  141. $format_handlers[] = 'name-full';
  142. }
  143. unset($widget_settings['name_format']);
  144. $widget_settings['format_handlers'] = $format_handlers;
  145. }
  146. // Update displays.
  147. foreach ($instance['display'] as $view_mode => &$view_mode_info) {
  148. $display_settings = &$view_mode_info['settings'];
  149. if ($view_mode_info['type'] == 'addressfield_default') {
  150. if (isset($widget_settings['format_handlers'])) {
  151. $display_settings['use_widget_handlers'] = 1;
  152. }
  153. else {
  154. // If the widget is non-standard, just use a sane default.
  155. $display_settings['use_widget_handlers'] = 0;
  156. $display_settings['format_handlers'] = array('address', 'name-oneline');
  157. }
  158. }
  159. else if ($view_mode_info['type'] == 'addressfield_name') {
  160. // Migrate the 'addressfield_name' formatter to the new framework.
  161. $view_mode_info['type'] = 'addressfield_default';
  162. // Start from the widget configuration.
  163. $display_settings['use_widget_handlers'] = 0;
  164. $display_settings['format_handlers'] = isset($widget_settings['format_handlers']) ? $widget_settings['format_handlers'] : array('address', 'name-oneline');
  165. if (empty($display_settings['organisation'])) {
  166. $display_settings['format_handlers'] = array_diff( $display_settings['format_handlers'], array('organisation'));
  167. }
  168. unset($display_settings['organisation']);
  169. }
  170. }
  171. field_update_instance($instance);
  172. }
  173. }
  174. }
  175. }
  176. /**
  177. * Sets the value of the new "Default country" setting.
  178. */
  179. function addressfield_update_7001() {
  180. $address_fields = array();
  181. foreach (field_info_fields() as $field_name => $field_info) {
  182. if ($field_info['type'] == 'addressfield') {
  183. $address_fields[$field_name] = $field_name;
  184. }
  185. }
  186. foreach (field_info_instances() as $entity_type => $bundles) {
  187. foreach ($bundles as $bundle_name => $instances) {
  188. foreach (array_intersect_key($instances, $address_fields) as $field_name => $instance) {
  189. // Optional fields get the None default. Required fields get the
  190. // previously selected default country.
  191. $default_country = '';
  192. if (!empty($instance['required']) && !empty($instance['default_value'])) {
  193. $default_country = $instance['default_value'][0]['country'];
  194. }
  195. $instance['widget']['settings']['default_country'] = $default_country;
  196. unset($instance['default_value']);
  197. field_update_instance($instance);
  198. }
  199. }
  200. }
  201. }