location.migrate.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @file
  4. * Migrate integration.
  5. */
  6. /**
  7. * Implements hook_migrate_api().
  8. */
  9. function location_migrate_api() {
  10. $api = array(
  11. 'api' => 2,
  12. 'field handlers' => array(
  13. 'MigrateLocationFieldHandler',
  14. ),
  15. );
  16. return $api;
  17. }
  18. /*
  19. *
  20. * Primary value passed to this field is the is_primary value (boolean)
  21. * but can be overwritten with subfields.
  22. *
  23. * Subfields are used to specify all the other values:
  24. * 'street'
  25. * 'additional',
  26. * 'city',
  27. * 'province',
  28. * 'postal_code',
  29. * 'country',
  30. * 'is_primary',
  31. * 'latitude',
  32. * 'longitude'
  33. *
  34. * @code
  35. * $this->addFieldMapping('field_location_dest')->defaultValue('TRUE');
  36. * $this->addFieldMapping('field_location_dest:street', 'field_source_address_1');
  37. * $this->addFieldMapping('field_location_dest:city', 'field_source_city');
  38. * @endcode
  39. */
  40. class MigrateLocationFieldHandler extends MigrateFieldHandler {
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function __construct() {
  45. $this->registerTypes(array('location'));
  46. }
  47. /**
  48. * Implementation of MigrateFieldHandler::fields().
  49. *
  50. * @param string $type
  51. * The file field type - 'file' or 'image'
  52. *
  53. * @param string $parent_field
  54. * Name of the parent field.
  55. *
  56. * @param Migration $migration
  57. * The migration context for the parent field. We can look at the mappings
  58. * and determine which subfields are relevant.
  59. *
  60. * @return array
  61. * Array of fields.
  62. */
  63. public function fields($type, $parent_field, $migration = NULL) {
  64. $dummy = array();
  65. $all_fields = location_invoke_locationapi($dummy, 'fields');
  66. unset($all_fields['locpick']);
  67. foreach ($all_fields as $field => $label) {
  68. $fields[$field] = t('Subfield: @label', array('@label' => $label));
  69. }
  70. $fields['latitude'] = t('Subfield: Latitude');
  71. $fields['longitude'] = t('Subfield: Longitude');
  72. $fields['source'] = t('Subfield: Source value');
  73. $fields['is_primary'] = t('Subfield: Is primary');
  74. return $fields;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function prepare($entity, array $field_info, array $instance, array $values) {
  80. $migration = Migration::currentMigration();
  81. $destination = $migration->getDestination();
  82. $arguments = array();
  83. if (isset($values['arguments'])) {
  84. $arguments = $values['arguments'];
  85. unset($values['arguments']);
  86. }
  87. $language = $this->getFieldLanguage($entity, $field_info, $arguments);
  88. $return = array($language => array());
  89. foreach ($values as $delta => $value) {
  90. // Handle potentially multiple arguments.
  91. $instance_arguments = array();
  92. foreach ($arguments as $key => $argument) {
  93. // For a scalar argument, pass it directly.
  94. if (!is_array($argument)) {
  95. if ($delta == 0) {
  96. $instance_arguments[$key] = $argument;
  97. }
  98. }
  99. elseif (isset($argument[$delta])) {
  100. $instance_arguments[$key] = $argument[$delta];
  101. }
  102. }
  103. if (isset($instance_arguments['country'])) {
  104. $instance_arguments['country'] = strtolower($instance_arguments['country']);
  105. }
  106. // Ensure we have at least one location field besides 'is_primary' before saving.
  107. if (!empty($instance_arguments)) {
  108. $instance_arguments['is_primary'] = ($delta == 0);
  109. $instance_arguments['inhibit_geocode'] = TRUE;
  110. // Only save the location if saved successfully.
  111. if ($lid = location_save($instance_arguments)) {
  112. $return[$language][$delta]['lid'] = $lid;
  113. }
  114. }
  115. }
  116. return $return;
  117. }
  118. }