Skip to content

Commit

Permalink
Merge pull request #122 from brandonkelly/wp-import-support
Browse files Browse the repository at this point in the history
Add an ACF adapter for Google Map fields
  • Loading branch information
lindseydiloreto authored Nov 11, 2024
2 parents b5aa61e + 744498e commit 2c31acf
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"require": {
"craftcms/cms": "^5.0.0"
},
"require-dev": {
"craftcms/wp-import": "dev-main"
},
"replace": {
"doublesecretagency/craft-smartmap":"3.*"
},
Expand Down
23 changes: 23 additions & 0 deletions src/GoogleMapsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,6 +102,7 @@ public function init(): void
$this->_registerFieldType();
$this->_registerCompatibleFieldTypes();
$this->_registerExporters();
$this->_registerAcfAdapter();

// Manage conversions of the Address field
$this->_manageFieldTypeConversions();
Expand Down Expand Up @@ -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.
*/
Expand Down
58 changes: 58 additions & 0 deletions src/acfadapters/GoogleMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Google Maps plugin for Craft CMS
*
* Maps in minutes. Powered by the Google Maps API.
*
* @author Double Secret Agency
* @author Brandon Kelly
* @link https://plugins.doublesecretagency.com/
* @copyright Copyright (c) 2014, 2024 Double Secret Agency
*/

namespace doublesecretagency\googlemaps\acfadapters;

use craft\base\FieldInterface;
use craft\wpimport\BaseAcfAdapter;
use doublesecretagency\googlemaps\enums\Defaults;
use doublesecretagency\googlemaps\fields\AddressField;

/**
* Class GoogleMap
*/
class GoogleMap extends BaseAcfAdapter
{
public static function type(): string
{
return 'google_map';
}

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

0 comments on commit 2c31acf

Please sign in to comment.