mirror of
https://github.com/a2nt/silverstripe-mapboxfield.git
synced 2024-10-22 12:05:53 +00:00
Initial updates
This commit is contained in:
parent
6ba6f62c7f
commit
82ef489899
93
README.md
93
README.md
@ -1,44 +1,101 @@
|
|||||||
# SilverStripe Mapbox Field
|
# SilverStripe Mapbox Field
|
||||||
|
|
||||||
|
Extends:
|
||||||
|
- Symbiote\Addressable\Geocodable
|
||||||
|
|
||||||
|
Replaces:
|
||||||
|
- dynamic/silverstripe-elemental-customer-service
|
||||||
|
- bigfork/silverstripe-mapboxfield
|
||||||
|
|
||||||
Adds a Mapbox map to the CMS with a draggable marker to allow content authors to add a location to a DataObject or Page.
|
Adds a Mapbox map to the CMS with a draggable marker to allow content authors to add a location to a DataObject or Page.
|
||||||
|
|
||||||
<img src="docs/img/cms.png" alt="" />
|
<img src="docs/img/cms.png" alt="" />
|
||||||
|
|
||||||
|
Address will be geocoded automatically or manually using mapbox map.
|
||||||
|
|
||||||
|
Adds DataObject Extension to store and render GeoJSON data.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
`composer require bigfork/silverstripe-mapboxfield:*`
|
`composer require a2nt/silverstripe-mapboxfield:*`
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
```yml
|
```yaml
|
||||||
|
|
||||||
---
|
---
|
||||||
Name: 'app-mapbox'
|
Name: 'app-map'
|
||||||
After: 'silverstripe-mapboxfield'
|
After:
|
||||||
|
- 'silverstripe-mapboxfield'
|
||||||
|
- 'addressable'
|
||||||
---
|
---
|
||||||
Bigfork\SilverStripeMapboxField\MapboxField:
|
SilverStripe\Core\Injector\Injector:
|
||||||
access_token: '<your mapbox key>'
|
A2nt\SilverStripeMapboxField\MarkerExtension:
|
||||||
|
properties:
|
||||||
|
geocoder: %$Symbiote\Addressable\MapboxGeocodeService
|
||||||
|
Symbiote\Addressable\GeocodeServiceInterface:
|
||||||
|
class: Symbiote\Addressable\MapboxGeocodeService
|
||||||
|
|
||||||
|
Symbiote\Addressable\MapboxGeocodeService:
|
||||||
|
mapbox_api_key: 'your mapbox access token'
|
||||||
|
A2nt\SilverStripeMapboxField\MapboxField:
|
||||||
|
map_style: 'mapbox://styles/mapbox/light-v10'
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```php
|
```php
|
||||||
class MyDataObject extends DataObject
|
|
||||||
|
class MyDataObjectMapPin extends DataObject
|
||||||
|
{
|
||||||
|
private static $extensions = [
|
||||||
|
Addressable::class,
|
||||||
|
MarkerExtension::class,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example
|
||||||
|
```php
|
||||||
|
class MyPage extends \Page
|
||||||
{
|
{
|
||||||
private static $db = [
|
private static $db = [
|
||||||
'Latitude' => 'Decimal(10, 8)',
|
'MapZoom' => 'Int',
|
||||||
'Longitude' => 'Decimal(11, 8)'
|
|
||||||
];
|
];
|
||||||
|
|
||||||
public function getCMSFields()
|
public function MapPins()
|
||||||
{
|
{
|
||||||
// ...
|
return MyDataObjectMapPin::get();
|
||||||
|
}
|
||||||
|
|
||||||
$fields->addFieldToTab(
|
public function getGeoJSON()
|
||||||
'Root.Map',
|
{
|
||||||
MapboxField::create('LocationMap', 'Choose a location', 'Latitude', 'Longitude')
|
$pins = [];
|
||||||
);
|
foreach ($this->MapPins() as $pin) {
|
||||||
|
$pins[] = $pin->getGeo();
|
||||||
// ...
|
}
|
||||||
|
return json_encode([
|
||||||
|
'type' => 'MarkerCollection',
|
||||||
|
'features' => $pins
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
create MyDataObjectMapPin.ss template to render popup content
|
||||||
|
|
||||||
|
MyPage.ss
|
||||||
|
```html
|
||||||
|
<div
|
||||||
|
class="mapAPI-map-container"
|
||||||
|
data-map-zoom="$MapZoom"
|
||||||
|
data-key="<% if $MapAPIKey %>$MapAPIKey<% else %>$SiteConfig.MapAPIKey<% end_if %>"
|
||||||
|
data-map-style="<% if $MapStyle %>$MapStyle<% else %>$SiteConfig.MapStyle<% end_if %>"
|
||||||
|
data-geojson="$GeoJSON.XML"
|
||||||
|
>
|
||||||
|
<div class="mapAPI-map"></div>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
|
||||||
|
Process it with javascript on frontend, you can find an example at: https://github.com/a2nt/webpack-bootstrap-ui-kit/blob/master/src/js/_components/_ui.map.api.js
|
||||||
|
@ -1,6 +1,18 @@
|
|||||||
---
|
---
|
||||||
Name: silverstripe-mapboxfield
|
Name: silverstripe-mapboxfield
|
||||||
---
|
---
|
||||||
|
|
||||||
|
SilverStripe\Core\Injector\Injector:
|
||||||
|
A2nt\SilverStripeMapboxField\MarkerExtension:
|
||||||
|
properties:
|
||||||
|
geocoder: %$Symbiote\Addressable\MapboxGeocodeService
|
||||||
|
Symbiote\Addressable\GeocodeServiceInterface:
|
||||||
|
class: Symbiote\Addressable\MapboxGeocodeService
|
||||||
|
|
||||||
|
SilverStripe\SiteConfig\SiteConfig:
|
||||||
|
extensions:
|
||||||
|
- A2nt\SilverStripeMapboxField\SiteConfigExtension
|
||||||
|
|
||||||
A2nt\SilverStripeMapboxField\MapboxField:
|
A2nt\SilverStripeMapboxField\MapboxField:
|
||||||
access_token: ''
|
access_token: ''
|
||||||
map_style: 'mapbox://styles/mapbox/basic-v9'
|
map_style: 'mapbox://styles/mapbox/basic-v9'
|
||||||
|
@ -5,19 +5,19 @@ namespace A2nt\SilverStripeMapboxField;
|
|||||||
use SilverStripe\Core\Config\Config;
|
use SilverStripe\Core\Config\Config;
|
||||||
use SilverStripe\Core\Extension;
|
use SilverStripe\Core\Extension;
|
||||||
use SilverStripe\View\Requirements;
|
use SilverStripe\View\Requirements;
|
||||||
|
use Symbiote\Addressable\MapboxGeocodeService;
|
||||||
|
|
||||||
class LeftAndMainExtension extends Extension
|
class LeftAndMainExtension extends Extension
|
||||||
{
|
{
|
||||||
public function onAfterInit()
|
public function onAfterInit()
|
||||||
{
|
{
|
||||||
$config = MapboxField::config();
|
$config = MapboxField::config();
|
||||||
|
$token = MapboxField::getAccessToken();
|
||||||
|
|
||||||
Requirements::css($config->api_css_url);
|
Requirements::css($config->api_css_url);
|
||||||
Requirements::javascript($config->api_javascript_url);
|
Requirements::javascript($config->api_javascript_url);
|
||||||
Requirements::css($config->geocoder_css_url);
|
Requirements::css($config->geocoder_css_url);
|
||||||
Requirements::javascript($config->geocoder_javascript_url);
|
Requirements::javascript($config->geocoder_javascript_url);
|
||||||
Requirements::customScript(<<<JS
|
Requirements::customScript('window.mapboxAccessToken = \''.$token.'\';');
|
||||||
window.mapboxAccessToken = '{$config->access_token}';
|
|
||||||
JS
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
|
|
||||||
namespace A2nt\SilverStripeMapboxField;
|
|
||||||
|
|
||||||
|
|
||||||
class MapExtension
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
@ -3,8 +3,10 @@
|
|||||||
namespace A2nt\SilverStripeMapboxField;
|
namespace A2nt\SilverStripeMapboxField;
|
||||||
|
|
||||||
use http\Exception\InvalidArgumentException;
|
use http\Exception\InvalidArgumentException;
|
||||||
|
use SilverStripe\Core\Config\Config;
|
||||||
use SilverStripe\Forms\CompositeField;
|
use SilverStripe\Forms\CompositeField;
|
||||||
use SilverStripe\Forms\HiddenField;
|
use SilverStripe\Forms\HiddenField;
|
||||||
|
use Symbiote\Addressable\MapboxGeocodeService;
|
||||||
|
|
||||||
class MapboxField extends CompositeField
|
class MapboxField extends CompositeField
|
||||||
{
|
{
|
||||||
@ -18,6 +20,14 @@ class MapboxField extends CompositeField
|
|||||||
|
|
||||||
private $curr_style;
|
private $curr_style;
|
||||||
|
|
||||||
|
public static function getAccessToken()
|
||||||
|
{
|
||||||
|
$config = MapboxField::config();
|
||||||
|
|
||||||
|
return $config->access_token
|
||||||
|
?: Config::inst()->get(MapboxGeocodeService::class, 'mapbox_api_key');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $title
|
* @param string $title
|
||||||
@ -28,8 +38,8 @@ class MapboxField extends CompositeField
|
|||||||
{
|
{
|
||||||
$cfg = self::config();
|
$cfg = self::config();
|
||||||
// check access_token
|
// check access_token
|
||||||
if(!$cfg->get('access_token')) {
|
if (!self::getAccessToken()) {
|
||||||
return new \ErrorException(self::class.': Please set Mapbox.com Access token');
|
return user_error(self::class.': Please set Mapbox.com Access token');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->curr_style = $cfg->get('map_style');
|
$this->curr_style = $cfg->get('map_style');
|
||||||
|
@ -3,13 +3,15 @@
|
|||||||
|
|
||||||
namespace A2nt\SilverStripeMapboxField;
|
namespace A2nt\SilverStripeMapboxField;
|
||||||
|
|
||||||
|
|
||||||
use SilverStripe\Forms\CheckboxField;
|
use SilverStripe\Forms\CheckboxField;
|
||||||
use SilverStripe\Forms\FieldList;
|
use SilverStripe\Forms\FieldList;
|
||||||
use Symbiote\Addressable\Geocodable;
|
use Symbiote\Addressable\Geocodable;
|
||||||
|
|
||||||
class MarkerExtension extends Geocodable
|
class MarkerExtension extends Geocodable
|
||||||
{
|
{
|
||||||
|
private static $icon = '<i class="fas fa-map-marker-alt"></i>';
|
||||||
|
private $curr_icon = null;
|
||||||
|
|
||||||
public function updateCMSFields(FieldList $fields)
|
public function updateCMSFields(FieldList $fields)
|
||||||
{
|
{
|
||||||
parent::updateCMSFields($fields);
|
parent::updateCMSFields($fields);
|
||||||
@ -17,6 +19,7 @@ class MarkerExtension extends Geocodable
|
|||||||
$record = $this->getOwner();
|
$record = $this->getOwner();
|
||||||
|
|
||||||
$fields->removeByName(['LatLngOverride', 'Lng','Lat']);
|
$fields->removeByName(['LatLngOverride', 'Lng','Lat']);
|
||||||
|
|
||||||
$fields->addFieldsToTab('Root.Map', [
|
$fields->addFieldsToTab('Root.Map', [
|
||||||
CheckboxField::create('LatLngOverride', 'Override Latitude and Longitude?')
|
CheckboxField::create('LatLngOverride', 'Override Latitude and Longitude?')
|
||||||
->setDescription('Check this box and save to be able to edit the latitude and longitude manually.'),
|
->setDescription('Check this box and save to be able to edit the latitude and longitude manually.'),
|
||||||
@ -31,6 +34,40 @@ class MarkerExtension extends Geocodable
|
|||||||
.$obj->getField('Lat').',' .$obj->getField('Lng');
|
.$obj->getField('Lat').',' .$obj->getField('Lng');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getIcon()
|
||||||
|
{
|
||||||
|
$obj = $this->owner;
|
||||||
|
$class = get_class($obj);
|
||||||
|
return $this->curr_icon ?: $class::config()->get('icon');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setIcon($icon)
|
||||||
|
{
|
||||||
|
$this->curr_icon = $icon;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getGeo()
|
||||||
|
{
|
||||||
|
$obj = $this->owner;
|
||||||
|
|
||||||
|
return [
|
||||||
|
'id' => $obj->ID,
|
||||||
|
'type' => 'Feature',
|
||||||
|
'icon' => $obj->getIcon(),
|
||||||
|
'properties' => [
|
||||||
|
'content' => $obj->forTemplate()->RAW(),
|
||||||
|
],
|
||||||
|
'geometry' => [
|
||||||
|
'type' => 'Point',
|
||||||
|
'coordinates' => [
|
||||||
|
$obj->getField('Lng'),
|
||||||
|
$obj->getField('Lat')
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function forTemplate()
|
public function forTemplate()
|
||||||
{
|
{
|
||||||
$obj = $this->owner;
|
$obj = $this->owner;
|
||||||
|
19
src/SiteConfigExtension.php
Normal file
19
src/SiteConfigExtension.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace A2nt\SilverStripeMapboxField;
|
||||||
|
|
||||||
|
use SilverStripe\Core\Extension;
|
||||||
|
|
||||||
|
class SiteConfigExtension extends Extension
|
||||||
|
{
|
||||||
|
public function MapAPIKey()
|
||||||
|
{
|
||||||
|
return MapboxField::getAccessToken();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function MapStyle()
|
||||||
|
{
|
||||||
|
return MapboxField::config()->get('map_style');
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user