addressfield.devel_generate.inc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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();
  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. $addresses = array();
  43. while (($buffer = fgets($handle)) !== false) {
  44. list($country, $administrative_area, $sub_administrative_area, $locality, $dependent_locality, $postal_code, $thoroughfare, $premise, $sub_premise) = explode("\t", $buffer);
  45. $fields[] = array(
  46. 'country' => ($country == 'NULL') ? NULL : trim($country),
  47. 'administrative_area' => ($administrative_area == 'NULL') ? NULL : trim($administrative_area),
  48. 'sub_administrative_area' => ($sub_administrative_area == 'NULL') ? NULL : trim($sub_administrative_area),
  49. 'locality' => ($locality == 'NULL') ? NULL : trim($locality),
  50. 'dependent_locality' => ($dependent_locality == 'NULL') ? NULL : trim($dependent_locality),
  51. 'postal_code' => ($postal_code == 'NULL') ? NULL : trim($postal_code),
  52. 'thoroughfare' => ($thoroughfare == 'NULL') ? NULL : trim($thoroughfare),
  53. 'premise' => ($premise == 'NULL') ? NULL : trim($premise),
  54. 'sub_premise' => ($sub_premise == 'NULL') ? NULL : trim($sub_premise),
  55. );
  56. }
  57. }
  58. fclose($handle);
  59. }
  60. }
  61. return $fields;
  62. }