1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\df_tools_map\Plugin\migrate\source; |
4
|
|
|
|
5
|
|
|
use Drupal\migrate\Row; |
6
|
|
|
use Drupal\migrate_source_csv\Plugin\migrate\source\CSV; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Source for Map block CSV. |
10
|
|
|
* |
11
|
|
|
* @MigrateSource( |
12
|
|
|
* id = "map_block" |
13
|
|
|
* ) |
14
|
|
|
*/ |
15
|
|
|
class MapBlock extends CSV { |
16
|
|
|
|
17
|
|
|
public function prepareRow(Row $row) { |
18
|
|
|
// Prefix the state per the normal Address schema. |
19
|
|
|
if ($state = $row->getSourceProperty('Address State')) { |
20
|
|
|
$row->setSourceProperty('Address State', 'US-' . $state); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
// Use a cached geolocation if possible. |
24
|
|
|
if ($geolocation = $row->getSourceProperty('Geolocation')) { |
25
|
|
|
list($lat, $lon) = explode(',', $geolocation); |
26
|
|
|
$point = [$lon, $lat]; |
27
|
|
|
$row->setSourceProperty('Geofield', \Drupal::service('geofield.wkt_generator')->WktBuildPoint($point)); |
28
|
|
|
} |
29
|
|
|
else { |
30
|
|
|
// Manually geocode from source information. |
31
|
|
|
$address = $row->getSourceProperty('Address Line 1'); |
32
|
|
|
$address2 = $row->getSourceProperty('Address Line 2'); |
33
|
|
|
$city = $row->getSourceProperty('Address City'); |
34
|
|
|
$zip = $row->getSourceProperty('Address Zip'); |
35
|
|
|
|
36
|
|
|
if ($address && $city && $zip && $state) { |
37
|
|
|
$address2 = $address2 ? $address2 . "\n" : ''; |
38
|
|
|
$address_string = "$address\n$address2$city, $state $zip\nUS"; |
39
|
|
|
if ($collection = \Drupal::service('geocoder')->geocode($address_string, ['googlemaps'])) { |
40
|
|
|
// Set our value in a similar way to Geofield's LatLon Widget. |
41
|
|
|
// @see \Drupal\geofield\Plugin\Field\FieldWidget\GeofieldLatLonWidget::massageFormValues() |
42
|
|
|
$coordinates = $collection->first()->getCoordinates(); |
43
|
|
|
$point = [ |
44
|
|
|
$coordinates->getLongitude(), |
45
|
|
|
$coordinates->getLatitude() |
46
|
|
|
]; |
47
|
|
|
$row->setSourceProperty('Geofield', \Drupal::service('geofield.wkt_generator') |
48
|
|
|
->WktBuildPoint($point)); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
} |
55
|
|
|
|