Last active
February 8, 2016 16:37
-
-
Save juancasantito/4a8a2a3253c487a5b1a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GmapJSONReader extends JSONReader { | |
public function getSourceFields($url) { | |
$items = parent::getSourceFields($url); | |
// Loop over the JSON values, walk the tree and extract as keyed values. | |
foreach ($items as &$item) { | |
if (isset($item['address_components']) && is_array($item['address_components'])) { | |
foreach ($item['address_components'] as $component) { | |
if (isset($component['long_name']) && isset($component['types']) && is_array($component['types'])) { | |
foreach ($component['types'] as $type) { | |
$item[$type] = $component['long_name']; | |
} | |
} | |
} | |
} | |
if (isset($item['geometry']['location']['lat'])) { | |
$item['lat'] = $item['geometry']['location']['lat']; | |
} | |
if (isset($item['geometry']['location']['lng'])) { | |
$item['lng'] = $item['geometry']['location']['lng']; | |
} | |
} | |
return $items; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GmapTypeJSONReader extends JSONReader { | |
public function getSourceFields($url) { | |
$items = parent::getSourceFields($url); | |
$return = []; | |
// Loop over the JSON values and extract types | |
// as individual rows. | |
foreach ($items as &$item) { | |
if (isset($item['types']) && is_array($item['types'])) { | |
foreach ($item['types'] as $type) | |
// We need to use place_id as the identifier for parsing JSON | |
// in the parent class. | |
// But we are really wanting to import an individual type | |
// only once. So over-write the place_id with the type. | |
$return[] = [ | |
'place_id' => $type, | |
'type' => $type, | |
]; | |
} | |
} | |
return $return; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id: gmap | |
migration_tags: JSON | |
label: gmap | |
source: | |
plugin: json_source | |
path: 'https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452' | |
headers: | |
Accept: application/json | |
identifier: place_id | |
identifierDepth: 1 | |
readerClass: Drupal\d8_json_migrate\Plugin\migrate\GmapJSONReader | |
fields: | |
- place_id | |
- types | |
- street_address | |
- postal_code | |
- country | |
- locality | |
- administrative_area_level_1 | |
process: | |
type: | |
plugin: default_value | |
default_value: gmaps | |
title: country | |
field_address_type: | |
plugin: migration | |
migration: gmap_type | |
source: types | |
field_city: locality | |
field_country: country | |
field_postal_code: postal_code | |
field_state: administrative_area_level_1 | |
field_street_address: street_address | |
destination: | |
plugin: 'entity:node' | |
template: null | |
migration_dependencies: {} | |
migration_group: JSON |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id: gmap_type | |
migration_tags: JSON | |
label: 'gmap type' | |
source: | |
plugin: json_source | |
path: 'https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452' | |
headers: | |
Accept: application/json | |
identifier: place_id | |
identifierDepth: 1 | |
readerClass: Drupal\d8_json_migrate\Plugin\migrate\GmapTypeJSONReader | |
fields: | |
- type | |
process: | |
name: type | |
vid: | |
plugin: default_value | |
default_value: address_type | |
destination: | |
plugin: 'entity:taxonomy_term' | |
template: null | |
migration_dependencies: { } | |
migration_group: JSON |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment