addressfield.devel_generate.inc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * @file
  4. * Devel Generate for addressfield module
  5. *
  6. */
  7. /*
  8. * Implements hook_devel_generate();
  9. */
  10. function addressfield_devel_generate($object, $field, $instance, $bundle) {
  11. if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
  12. return devel_generate_multiple('_addressfield_devel_generate', $object, $field, $instance, $bundle);
  13. }
  14. else {
  15. return _addressfield_devel_generate($object, $field, $instance, $bundle);
  16. }
  17. }
  18. /*
  19. * Helper function to generate a greeky US-based address
  20. */
  21. function _addressfield_devel_generate($object, $field, $instance, $bundle) {
  22. // Set default value
  23. $object_field = addressfield_default_values($field, $instance);
  24. $addresses = _addressfield_sample_addresses();
  25. $object_field = array_merge($object_field, $addresses[array_rand($addresses)]);
  26. unset($object_field['data']);
  27. return $object_field;
  28. }
  29. /**
  30. * Returns an array of sample addresses.
  31. *
  32. * @return
  33. * An array of addressfield arrays
  34. */
  35. function _addressfield_sample_addresses() {
  36. $fields = &drupal_static(__FUNCTION__);
  37. if (!isset($fields)) {
  38. $filepath = DRUPAL_ROOT . '/' . drupal_get_path('module', 'addressfield');
  39. $fields = array();
  40. if ($handle = @fopen("$filepath/addresses.txt", 'r')) {
  41. if (is_resource($handle)) {
  42. while (($buffer = fgets($handle)) !== FALSE) {
  43. list($country, $administrative_area, $sub_administrative_area, $locality, $dependent_locality, $postal_code, $thoroughfare, $premise, $sub_premise) = explode("\t", $buffer);
  44. $fields[] = array(
  45. 'country' => ($country == 'NULL') ? NULL : trim($country),
  46. 'administrative_area' => ($administrative_area == 'NULL') ? NULL : trim($administrative_area),
  47. 'sub_administrative_area' => ($sub_administrative_area == 'NULL') ? NULL : trim($sub_administrative_area),
  48. 'locality' => ($locality == 'NULL') ? NULL : trim($locality),
  49. 'dependent_locality' => ($dependent_locality == 'NULL') ? NULL : trim($dependent_locality),
  50. 'postal_code' => ($postal_code == 'NULL') ? NULL : trim($postal_code),
  51. 'thoroughfare' => ($thoroughfare == 'NULL') ? NULL : trim($thoroughfare),
  52. 'premise' => ($premise == 'NULL') ? NULL : trim($premise),
  53. 'sub_premise' => ($sub_premise == 'NULL') ? NULL : trim($sub_premise),
  54. );
  55. }
  56. }
  57. fclose($handle);
  58. }
  59. }
  60. return $fields;
  61. }