geofield.migrate.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * @file
  4. * Support for migration into Geofield fields.
  5. *
  6. * Based on addressfield.migrate.inc
  7. * in the Address Field module.
  8. */
  9. /**
  10. * Implements hook_migrate_api().
  11. */
  12. function geofield_migrate_api() {
  13. return array(
  14. 'api' => 2,
  15. 'field handlers' => array('MigrateGeofieldFieldHandler'),
  16. );
  17. }
  18. /**
  19. * Primary value passed to this field must be the Geometry type string.
  20. *
  21. * Arguments are used to specify all the other values.
  22. *
  23. * Add the source field mappings to the argument array then add null mappings to
  24. * avoid having fields flagged as as unmapped:
  25. * @code
  26. *
  27. * $this->addFieldMapping('field_geofield', 'foo');
  28. * $this->addFieldMapping('field_geofield:wkt', 'wkt');
  29. * $this->addFieldMapping('field_geofield:lat', 'lat');
  30. * $this->addFieldMapping('field_geofield:lon', 'lon');
  31. * @endcode
  32. */
  33. class MigrateGeofieldFieldHandler extends MigrateFieldHandler {
  34. /**
  35. * Registers the geofield field type.
  36. */
  37. public function __construct() {
  38. $this->registerTypes(array('geofield'));
  39. }
  40. /**
  41. * Implementation of MigrateFieldHandler::fields().
  42. *
  43. * @param string $type
  44. * The field type.
  45. * @param array $instance
  46. * Instance info for the field.
  47. * @param object $migration
  48. * The migration context for the parent field. We can look at the mappings
  49. * and determine which subfields are relevant.
  50. *
  51. * @return array
  52. * List of subfields.
  53. */
  54. public function fields($type, $instance, $migration = NULL) {
  55. return array(
  56. 'input_format' => t('Subfield: Input Format'),
  57. 'wkt' => t('Subfield: WKT'),
  58. 'geo_type' => t('Subfield: Geo_type'),
  59. 'json' => t('Subfield: GeoJSON'),
  60. 'lat' => t('Subfield: Lat'),
  61. 'lon' => t('Subfield: Lon'),
  62. 'left' => t('Subfield: Left'),
  63. 'top' => t('Subfield: Top'),
  64. 'right' => t('Subfield: Right'),
  65. 'bottom' => t('Subfield: Bottom'),
  66. );
  67. }
  68. /**
  69. * Implements MigrateFieldHandler::prepare().
  70. *
  71. * @param object $entity
  72. * Entity which is going to embed the field.
  73. * @param array $field_info
  74. * Info for the field.
  75. * @param array $instance
  76. * Instance info for the field.
  77. * @param array $values
  78. * Values to be stored in the field.
  79. */
  80. public function prepare($entity, array $field_info, array $instance, array $values) {
  81. $arguments = array();
  82. if (isset($values['arguments'])) {
  83. $arguments = array_filter($values['arguments']);
  84. unset($values['arguments']);
  85. }
  86. $language = $this->getFieldLanguage($entity, $field_info, $arguments);
  87. // Setup a Field API array for saving.
  88. // Use https://drupal.org/node/1854188 as a guide for geoPHP field type.
  89. $delta = 0;
  90. foreach ($values as $value) {
  91. $input_format = $arguments['input_format'];
  92. if (isset($input_format)) {
  93. $return[$language][$delta]['input_format'] = $arguments['input_format'];
  94. switch ($input_format) {
  95. case 'wkt':
  96. // Well known text - array of WKT data.
  97. $return[$language][$delta]['geom'] = $value;
  98. break;
  99. case 'json':
  100. // GeoJSON - requires array of JSON data.
  101. $return[$language][$delta]['geom'] = $value;
  102. break;
  103. case 'lat/lon':
  104. // Lat/Long - requires array of lat and lon source fields.
  105. $return[$language][$delta]['geom']['lat'] = $arguments['lat'];
  106. $return[$language][$delta]['geom']['lon'] = $arguments['lon'];
  107. break;
  108. case 'bounds':
  109. // Delimited area marked by an array of left (lon), top (lat), right
  110. // (long), bottom (lat) values.
  111. $return[$language][$delta]['geom']['left'] = $arguments['left'];
  112. $return[$language][$delta]['geom']['top'] = $arguments['top'];
  113. $return[$language][$delta]['geom']['right'] = $arguments['right'];
  114. $return[$language][$delta]['geom']['bottom'] = $arguments['bottom'];
  115. break;
  116. }
  117. }
  118. else {
  119. // If no input format specified the $value is assumed to represent a
  120. // a WKT value or another format that can be parsed by geoPHP.
  121. $return[$language][$delta]['geom'] = $value;
  122. }
  123. $delta++;
  124. }
  125. return isset($return) ? $return : NULL;
  126. }
  127. /**
  128. * Builds an array with additional data for the current $delta.
  129. *
  130. * @param array $arguments
  131. * Arguments.
  132. * @param array $field_info
  133. * Info for the field.
  134. * @param int $delta
  135. * Order of value (typically for multi-valued fields).
  136. *
  137. * @return array
  138. * Array with additional data.
  139. */
  140. protected function prepareArguments(array $arguments, array $field_info, $delta) {
  141. $result = array();
  142. foreach ($arguments as $column_key => $column_value) {
  143. $value = NULL;
  144. if (is_array($arguments[$column_key])) {
  145. if (!empty($arguments[$column_key][$delta])) {
  146. $value = $arguments[$column_key][$delta];
  147. }
  148. }
  149. else {
  150. $value = $arguments[$column_key];
  151. }
  152. if ($value && isset($field_info['columns'][$column_key])) {
  153. $result[$column_key] = $value;
  154. }
  155. }
  156. return $result;
  157. }
  158. }