Location Field Destination Handler for Migrate API

Public

This class registers location cck field as a destination for Migrate module. Just put it somewhere in your custom migrate module:

</> CopyGet raw version
php
  1. class LocationFieldHandler extends MigrateFieldHandler {
  2. public function __construct() {
  3. $this->registerTypes(array('location'));
  4. }
  5.  
  6. /**
  7.   * Implementation of MigrateFieldHandler::fields().
  8.   *
  9.   * @param $type
  10.   * The file field type - 'file' or 'image'
  11.   * @param $parent_field
  12.   * Name of the parent field.
  13.   * @param Migration $migration
  14.   * The migration context for the parent field. We can look at the mappings
  15.   * and determine which subfields are relevant.
  16.   * @return array
  17.   */
  18. public function fields($type, $parent_field, $migration = NULL) {
  19.  
  20. $fields = array(
  21. 'name' => t('Subfield: String to be used as the name value'),
  22. 'street' => t('Subfield: String to be used as the street value'),
  23. 'additional' => t('Subfield: String to be used as the additional value'),
  24. 'city' => t('Subfield: String to be used as the city value'),
  25. 'province' => t('Subfield: String to be used as the province value'),
  26. 'postal_code' => t('Subfield: String to be used as the postal code value'),
  27. 'latitude' => t('Subfield: Float to be used as the latitude value'),
  28. 'longitude' => t('Subfield: Float to be used as the longitude value'),
  29. 'country' => t('Subfield: String (ISO code) to be used as the country value'),
  30. 'source' => t('Subfield: String to be used as the source value'),
  31. 'is_primary' => t('Subfield: Boolean to be used as the is primary value'),
  32. );
  33. return $fields;
  34. }
  35.  
  36. public function prepare($entity, array $field_info, array $instance, array $values) {
  37. $migration = Migration::currentMigration();
  38. $destination = $migration->getDestination();
  39.  
  40. $arguments = array();
  41. if (isset($values['arguments'])) {
  42. $arguments = $values['arguments'];
  43. unset($values['arguments']);
  44. }
  45.  
  46. $language = $this->getFieldLanguage($entity, $field_info, $arguments);
  47. $return = array($language => array());
  48.  
  49. foreach ($values as $delta => $value) {
  50. // Handle potentially multiple arguments.
  51. $instance_arguments = array();
  52.  
  53. foreach ($arguments as $key => $argument) {
  54. // For a scalar argument, pass it directly.
  55. if (!is_array($argument)) {
  56. if ($delta == 0) {
  57. $instance_arguments[$key] = $argument;
  58. }
  59. }
  60. elseif (isset($argument[$delta])) {
  61. $instance_arguments[$key] = $argument[$delta];
  62. }
  63. }
  64.  
  65. if (isset($instance_arguments['country'])) {
  66. $instance_arguments['country'] = strtolower($instance_arguments['country']);
  67. }
  68.  
  69. // Ensure we have at least one location field besides 'is_primary' before saving
  70. if (!empty($instance_arguments)) {
  71. $instance_arguments['is_primary'] = $value;
  72.  
  73. // Only save the location if saved successfully.
  74. if ($lid = location_save($instance_arguments)) {
  75. $return[$language][$delta]['lid'] = $lid;
  76. }
  77. }
  78. }
  79.  
  80. return $return;
  81. }
  82.  
  83. }