Skip to content

Commit

Permalink
Can convert Address fields from Mapbox plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
lindseydiloreto committed Oct 17, 2023
1 parent f123fe1 commit 6415af3
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added
- Address fields can now be added to the search index. ([#21](https://github.com/doublesecretagency/craft-googlemaps/issues/21))
- Optionally use minified JavaScript files for dynamic maps on the front-end. ([#98](https://github.com/doublesecretagency/craft-googlemaps/issues/98))
- Added ability to [convert Address fields](https://plugins.doublesecretagency.com/google-maps/guides/converting-from-mapbox/) from the Mapbox plugin.

## 4.3.6 - 2023-09-18

Expand Down
2 changes: 2 additions & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = {
{text: 'Linking to a Map', link: '/guides/linking-to-a-map/'},
{text: 'Address in a Matrix Field', link: '/guides/address-in-a-matrix-field/'},
{text: 'Importing Addresses', link: '/guides/importing-addresses/'},
{text: 'Converting from Mapbox', link: '/guides/converting-from-mapbox/'},
] },
{ text: 'Dynamic Maps', items: [
{text: 'Troubleshoot Dynamic Maps', link: '/guides/troubleshoot-dynamic-maps/'},
Expand Down Expand Up @@ -238,6 +239,7 @@ module.exports = {
'linking-to-a-map',
'address-in-a-matrix-field',
'importing-addresses',
'converting-from-mapbox',
]
},
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions docs/guides/converting-from-mapbox.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
description: If migrating from the Mapbox plugin, it's very simple to import all of your existing Address data into the Google Maps plugin.
meta:
- property: og:type
content: website
- property: og:url
content: https://plugins.doublesecretagency.com/google-maps/guides/converting-from-mapbox/
- property: og:title
content: Converting from Mapbox | Google Maps plugin for Craft CMS
- property: og:description
content: If migrating from the Mapbox plugin, it's very simple to import all of your existing Address data into the Google Maps plugin.
- property: og:image
content: https://plugins.doublesecretagency.com/google-maps/images/guides/switch-field-type.png
- property: twitter:card
content: summary_large_image
- property: twitter:url
content: https://plugins.doublesecretagency.com/google-maps/guides/converting-from-mapbox/
- property: twitter:title
content: Converting from Mapbox | Google Maps plugin for Craft CMS
- property: twitter:description
content: If migrating from the Mapbox plugin, it's very simple to import all of your existing Address data into the Google Maps plugin.
- property: twitter:image
content: https://plugins.doublesecretagency.com/google-maps/images/guides/switch-field-type.png
---

# Converting from Mapbox

## Importing Address Data

Converting from an "Address (Mapbox)" field to an "Address (Google Maps)" field is relatively straightforward... simply **switch the field type** on the field's settings page.

<img class="dropshadow" :src="$withBase('/images/guides/switch-field-type.png')" alt="Screenshot of field type select being switched to Address (Google Maps)" width="233" style="margin-left:20px; margin-bottom:4px;">

:::warning Ignorable Warning ⚠️
You might be warned that these two field types are not compatible, but they actually are!

It is safe to ignore the warning about field type compatibility.
:::

Once you have saved the field as an "Address (Google Maps)" field, all data associated with that field will automatically be imported into the Google Maps plugin.

### One Field at a Time

When you update a single field, only the data for that specific field will be converted. Each Address field will need to be updated individually.

:::tip Field Configuration Not Included
The field's **settings** will not be ported, only the field's existing **data** will be transferred over.

You may still want to configure the Google Maps field to your liking, it will not automatically reflect how you had it configured with Mapbox.
:::

### Deploying to Production

When deploying to a production environment, you'll most likely be using [Project Config](https://craftcms.com/docs/4.x/project-config.html) to keep all of your configuration settings in sync. Don't worry, there will be little (or no) action required on your part.

All relevant Address data will be converted **once your Project Config changes are applied**.
85 changes: 85 additions & 0 deletions src/GoogleMapsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
use craft\base\Model;
use craft\base\Plugin;
use craft\elements\Entry;
use craft\events\ConfigEvent;
use craft\events\ModelEvent;
use craft\events\PluginEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterElementExportersEvent;
use craft\helpers\UrlHelper;
use craft\services\Fields;
use craft\services\Plugins;
use craft\services\ProjectConfig;
use craft\services\Utilities;
use doublesecretagency\googlemaps\exporters\AddressesCondensedExporter;
use doublesecretagency\googlemaps\exporters\AddressesExpandedExporter;
Expand Down Expand Up @@ -97,6 +99,9 @@ public function init(): void
$this->_registerFieldType();
$this->_registerExporters();

// Manage conversions of the Address field
$this->_manageFieldTypeConversions();

// Redirect after plugin install
$this->_postInstallRedirect();

Expand Down Expand Up @@ -183,6 +188,86 @@ static function(RegisterComponentTypesEvent $event) {

// ========================================================================= //

/**
* Manage conversions of the Address field from the Mapbox plugin.
*
* @return void
*/
private function _manageFieldTypeConversions(): void
{
// If Mapbox plugin is not installed, bail
if (!Craft::$app->getPlugins()->isPluginEnabled('mapbox')) {
return;
}

// When a single project config line gets updated
Event::on(
ProjectConfig::class,
ProjectConfig::EVENT_UPDATE_ITEM,
static function (ConfigEvent $event) {

// Get old and new types
$oldType = $event->oldValue['type'] ?? null;
$newType = $event->newValue['type'] ?? null;

// If old type wasn't a Mapbox Address field, bail
if (!($oldType === 'doublesecretagency\mapbox\fields\AddressField')) {
return;
}

// If new type is not a Google Maps Address field, bail
if (!($newType === 'doublesecretagency\googlemaps\fields\AddressField')) {
return;
}

// Get the field's UID
$uid = str_replace('fields.', '', $event->path);

// Get the actual field
$field = Craft::$app->getFields()->getFieldByUid($uid);

// If unable to get the field, bail
if (!$field) {
return;
}

// List of columns to copy between tables
$columns = [
'elementId', 'fieldId',
'formatted', 'raw',
'name', 'street1', 'street2',
'city', 'state', 'zip',
'county', 'country',
'lat', 'lng', 'zoom',
'dateCreated', 'dateUpdated', 'uid'
];

// Merge and escape column names
$columns = '[['.implode(']],[[', $columns).']]';

// Copy field's rows from `mapbox_addresses` into `googlemaps_addresses`
$sql = "
INSERT INTO [[googlemaps_addresses]] ({$columns})
SELECT {$columns}
FROM [[mapbox_addresses]]
WHERE [[fieldId]] = :fieldId
AND NOT EXISTS (
SELECT 1
FROM [[googlemaps_addresses]]
WHERE [[googlemaps_addresses]].[[uid]] = [[mapbox_addresses]].[[uid]]
)";

// Execute the SQL statement
\Yii::$app->db->createCommand($sql)
->bindValues([':fieldId' => $field->id])
->execute();

}
);
}

// ========================================================================= //

/**
* After the plugin has been installed,
* redirect to the "Welcome" settings page.
Expand Down
2 changes: 1 addition & 1 deletion src/fields/AddressField.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class AddressField extends Field implements PreviewableFieldInterface
*/
public static function displayName(): string
{
return Craft::t('google-maps', 'Address');
return Craft::t('google-maps', 'Address (Google Maps)');
}

/**
Expand Down

0 comments on commit 6415af3

Please sign in to comment.