From d22ad836547b049ea5b6fc06bdb3c20cc889830a Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Mon, 4 Nov 2024 05:21:50 -0800 Subject: [PATCH] Add an ACF adapter for Google Map fields --- composer.json | 3 ++ src/SimpleMap.php | 12 +++++++ src/acfadapters/GoogleMap.php | 66 +++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 src/acfadapters/GoogleMap.php diff --git a/composer.json b/composer.json index f040b74..7f6d886 100644 --- a/composer.json +++ b/composer.json @@ -15,6 +15,9 @@ "ext-zlib": "*", "php": "^8.2" }, + "require-dev": { + "craftcms/wp-import": "dev-main" + }, "autoload": { "psr-4": { "ether\\simplemap\\": "src/" diff --git a/src/SimpleMap.php b/src/SimpleMap.php index 9d1771e..1694a07 100644 --- a/src/SimpleMap.php +++ b/src/SimpleMap.php @@ -20,6 +20,8 @@ use craft\web\Application; use craft\web\twig\variables\CraftVariable; use craft\web\UrlManager; +use craft\wpimport\Command as WpImportCommand; +use ether\simplemap\acfadapters\GoogleMap as GoogleMapAcfAdapter; use ether\simplemap\fields\MapField as MapField; use ether\simplemap\integrations\feedme\FeedMeMaps; use ether\simplemap\integrations\graphql\MapPartsType; @@ -140,6 +142,16 @@ public function init () [$this, 'onApplicationInit'] ); } + + if (class_exists(WpImportCommand::class)) { + Event::on( + WpImportCommand::class, + WpImportCommand::EVENT_REGISTER_ACF_ADAPTERS, + static function (RegisterComponentTypesEvent $event) { + $event->types[] = GoogleMapAcfAdapter::class; + } + ); + } } protected function beforeUninstall (): void diff --git a/src/acfadapters/GoogleMap.php b/src/acfadapters/GoogleMap.php new file mode 100644 index 0000000..da70792 --- /dev/null +++ b/src/acfadapters/GoogleMap.php @@ -0,0 +1,66 @@ +lat = $data['center_lat']; + } + if ($data['center_lng']) { + $field->lng = $data['center_lng']; + } + if ($data['zoom']) { + $field->zoom = $data['zoom']; + } + return $field; + } + + public function normalizeValue(mixed $value, array $data): mixed + { + return [ + 'address' => $value['address'], + 'lat' => $value['lat'], + 'lng' => $value['lng'], + 'zoom' => $value['zoom'], + 'parts' => [ + 'number' => $value['street_number'], + 'address' => $value['street_name'], + 'city' => $value['city'], + 'postcode' => $value['post_code'], + 'state' => $value['state'], + 'country' => $value['country'], + 'administrative_area_level_1' => $value['state'], + 'locality' => $value['city'], + 'postal_code' => $value['post_code'], + 'route' => $value['street_name'], + 'street_number' => $value['street_number'], + ], + ]; + } +}