From 744498ef385cf2bf8e4b25af2b0dc2e626150b04 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Mon, 4 Nov 2024 05:20:59 -0800 Subject: [PATCH] Add an ACF adapter for Google Map fields --- composer.json | 3 ++ src/GoogleMapsPlugin.php | 23 ++++++++++++++ src/acfadapters/GoogleMap.php | 58 +++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/acfadapters/GoogleMap.php diff --git a/composer.json b/composer.json index 37efad3..b13980b 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,9 @@ "require": { "craftcms/cms": "^5.0.0" }, + "require-dev": { + "craftcms/wp-import": "dev-main" + }, "replace": { "doublesecretagency/craft-smartmap":"3.*" }, diff --git a/src/GoogleMapsPlugin.php b/src/GoogleMapsPlugin.php index 591b0b0..ce97f62 100644 --- a/src/GoogleMapsPlugin.php +++ b/src/GoogleMapsPlugin.php @@ -28,6 +28,8 @@ use craft\services\Plugins; use craft\services\ProjectConfig; use craft\services\Utilities; +use craft\wpimport\Command as WpImportCommand; +use doublesecretagency\googlemaps\acfadapters\GoogleMap as GoogleMapAcfAdapter; use doublesecretagency\googlemaps\exporters\AddressesCondensedExporter; use doublesecretagency\googlemaps\exporters\AddressesExpandedExporter; use doublesecretagency\googlemaps\fields\AddressField; @@ -100,6 +102,7 @@ public function init(): void $this->_registerFieldType(); $this->_registerCompatibleFieldTypes(); $this->_registerExporters(); + $this->_registerAcfAdapter(); // Manage conversions of the Address field $this->_manageFieldTypeConversions(); @@ -199,6 +202,26 @@ static function (RegisterElementExportersEvent $event) { ); } + /** + * Register the ACF adapter for wp-import. + * + * @return void + */ + private function _registerAcfAdapter(): void + { + if (!class_exists(WpImportCommand::class)) { + return; + } + + Event::on( + WpImportCommand::class, + WpImportCommand::EVENT_REGISTER_ACF_ADAPTERS, + static function (RegisterComponentTypesEvent $event) { + $event->types[] = GoogleMapAcfAdapter::class; + } + ); + } + /** * Register utilities. */ diff --git a/src/acfadapters/GoogleMap.php b/src/acfadapters/GoogleMap.php new file mode 100644 index 0000000..61c307d --- /dev/null +++ b/src/acfadapters/GoogleMap.php @@ -0,0 +1,58 @@ +coordinatesDefault = [ + 'lat' => $data['center_lat'] ?: Defaults::COORDINATES['lat'], + 'lng' => $data['center_lng'] ?: Defaults::COORDINATES['lng'], + 'zoom' => $data['zoom'] ?: Defaults::COORDINATES['zoom'], + ]; + return $field; + } + + public function normalizeValue(mixed $value, array $data): mixed + { + return [ + 'lat' => $value['lat'], + 'lng' => $value['lng'], + 'zoom' => $value['zoom'], + 'formatted' => $value['address'], + 'name' => $value['name'], + 'street1' => sprintf('%s %s', $value['street_number'], $value['street_name_short'] ?: $value['street_name']), + 'city' => $value['city'], + 'state' => $value['state_short'], + 'zip' => $value['post_code'], + 'country' => $value['country'], + 'countryCode' => $value['country_short'], + 'placeId' => $value['place_id'], + ]; + } +}