location.migrate.inc in Location 7.3
File
location.migrate.inc
View source
<?php
function location_migrate_api() {
$api = array(
'api' => 2,
'field handlers' => array(
'MigrateLocationFieldHandler',
),
);
return $api;
}
class MigrateLocationFieldHandler extends MigrateFieldHandler {
public function __construct() {
$this
->registerTypes(array(
'location',
));
}
public function fields($type, $parent_field, $migration = NULL) {
$dummy = array();
$all_fields = location_invoke_locationapi($dummy, 'fields');
unset($all_fields['locpick']);
foreach ($all_fields as $field => $label) {
$fields[$field] = t('Subfield: @label', array(
'@label' => $label,
));
}
$fields['latitude'] = t('Subfield: Latitude');
$fields['longitude'] = t('Subfield: Longitude');
$fields['source'] = t('Subfield: Source value');
$fields['is_primary'] = t('Subfield: Is primary');
return $fields;
}
public function prepare($entity, array $field_info, array $instance, array $values) {
$migration = Migration::currentMigration();
$destination = $migration
->getDestination();
$arguments = array();
if (isset($values['arguments'])) {
$arguments = $values['arguments'];
unset($values['arguments']);
}
$language = $this
->getFieldLanguage($entity, $field_info, $arguments);
$return = array(
$language => array(),
);
foreach ($values as $delta => $value) {
$instance_arguments = array();
foreach ($arguments as $key => $argument) {
if (!is_array($argument)) {
if ($delta == 0) {
$instance_arguments[$key] = $argument;
}
}
elseif (isset($argument[$delta])) {
$instance_arguments[$key] = $argument[$delta];
}
}
if (isset($instance_arguments['country'])) {
$instance_arguments['country'] = strtolower($instance_arguments['country']);
}
if (!empty($instance_arguments)) {
$instance_arguments['is_primary'] = $delta == 0;
$instance_arguments['inhibit_geocode'] = TRUE;
if ($lid = location_save($instance_arguments)) {
$return[$language][$delta] = array_merge(array(
'lid' => $lid,
), $instance_arguments);
}
}
}
return $return;
}
}