Skip to content

Commit

Permalink
Add an ACF adapter for Google Map fields
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Nov 4, 2024
1 parent 4350fac commit d22ad83
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"ext-zlib": "*",
"php": "^8.2"
},
"require-dev": {
"craftcms/wp-import": "dev-main"
},
"autoload": {
"psr-4": {
"ether\\simplemap\\": "src/"
Expand Down
12 changes: 12 additions & 0 deletions src/SimpleMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions src/acfadapters/GoogleMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Maps for Craft CMS
*
* @link https://ethercreative.co.uk
* @copyright Copyright (c) 2024 Ether Creative
*/

namespace ether\simplemap\acfadapters;

use craft\base\FieldInterface;
use craft\wpimport\BaseAcfAdapter;
use ether\simplemap\fields\MapField;

/**
* Class GoogleMap
*
* @author Ether Creative
* @author Brandon Kelly
* @package ether\simplemap\acfadapters
*/
class GoogleMap extends BaseAcfAdapter
{
public static function type(): string
{
return 'google_map';
}

public function create(array $data): FieldInterface
{
$field = new MapField();
if ($data['center_lat']) {
$field->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'],
],
];
}
}

0 comments on commit d22ad83

Please sign in to comment.