Commit cdf122af authored by itamair's avatar itamair
Browse files

Added LeafletTileLayersPlugin Type

Default LeafletTileLayerPlugins from leaflet-extras/leaflet-providers defined
Issue #2946851 by anruether: Leaflet Form Formatter loads non-ssl map data
Readme.md Updated
parent ca8965b3
......@@ -88,6 +88,7 @@ Format settings for specific personalization;
should be able to configure/edit the module settings.
####Hints for Advanced Use:
- GeofieldMapWidget uses Leaflet MapTypes/Tiles pre-defined as LeafletTileLayers D8 plugins, but any third party module is able to define and add its new LeafletTileLayer Plugins.
- As default (configurable) option, eventual overlapping markers will be
Spiderfied, with the support of the
[Overlapping Marker Spiderfier Library (for Google Maps)](https://github.com/jawj/OverlappingMarkerSpiderfier#overlapping-marker-spiderfier-for-google-maps-api-v3)
......
services:
plugin.manager.leaflet_tile_layer_plugin:
class: Drupal\geofield_map\LeafletTileLayer\LeafletTileLayerPluginManager
parent: default_plugin_manager
arguments: ['@request_stack']
<?php
namespace Drupal\geofield_map\Annotation;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a LeafletTileLayerPlugin item annotation object.
*
* @see \Drupal\geofield_map\leafletTileLayers\LeafletTileLayerPluginManager
* @see plugin_api
*
* @Annotation
*/
class LeafletTileLayerPlugin extends Plugin {
/**
* The Leaflet Tile Layer plugin ID.
*
* @var string
*/
public $id;
/**
* The label of the plugin.
*
* @var \Drupal\Core\Annotation\Translation
*
* @ingroup plugin_translatable
*/
public $label;
/**
* The url of the Leaflet Tile Layer.
*
* @var string
*/
public $url;
/**
* The options array for the Leaflet Tile Layer plugin.
*
* @var array
*/
public $options = [];
}
<?php
namespace Drupal\geofield_map\leafletTileLayers;
use Drupal\Component\Plugin\PluginBase;
/**
* Base class for Leaflet tile layers plugin plugins.
*/
abstract class LeafletTileLayerPluginBase extends PluginBase implements LeafletTileLayerPluginInterface {
// Add common methods and abstract methods for your plugin type here.
}
<?php
namespace Drupal\geofield_map\leafletTileLayers;
use Drupal\Component\Plugin\PluginInspectionInterface;
/**
* Defines an interface for Leaflet tile layers plugin plugins.
*/
interface LeafletTileLayerPluginInterface extends PluginInspectionInterface {
// Add get/set methods for your plugin type here.
}
<?php
namespace Drupal\geofield_map\leafletTileLayer;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Provides the Leaflet tile layers plugin plugin manager.
*/
class LeafletTileLayerPluginManager extends DefaultPluginManager {
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a new leafletTileLayersPluginManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, RequestStack $request_stack) {
parent::__construct('Plugin/LeafletTileLayerPlugin', $namespaces, $module_handler, 'Drupal\geofield_map\LeafletTileLayer\LeafletTileLayerPluginInterface', 'Drupal\geofield_map\Annotation\LeafletTileLayerPlugin');
$this->alterInfo('geofield_map_leaflet_tile_layer_plugin_info');
$this->setCacheBackend($cache_backend, 'geofield_map_leaflet_tile_layer_plugin_plugins');
$this->requestStack = $request_stack;
}
/**
* Get the associative array of all defined Leaflet Tile Layers.
*/
public function getLeafletTileLayers() {
$leaflet_tile_layers = [];
foreach ($this->getDefinitions() as $k => $plugin) {
// Change tile url protocol if under secure request (Ssl).
$plugin['url'] = $this->requestStack->getCurrentRequest()->isSecure() ? preg_replace("/^http:/i", "https:", $plugin['url']) : $plugin['url'];
$leaflet_tile_layers[$k] = [
'label' => $plugin['label'],
'url' => $plugin['url'],
'options' => $plugin['options'],
];
}
return $leaflet_tile_layers;
}
/**
* Get the id => label associative array of all defined Leaflet Tile Layers.
*/
public function getLeafletTilesLayersOptions() {
$options = [];
foreach ($this->getDefinitions() as $k => $plugin) {
$options[$k] = $plugin['label'];
}
ksort($options);
return $options;
}
}
......@@ -17,6 +17,8 @@ use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\geofield\GeoPHP\GeoPHPInterface;
use Drupal\geofield\WktGeneratorInterface;
use Drupal\geofield_map\leafletTileLayer\LeafletTileLayerPluginManager;
use Drupal\Core\Session\AccountInterface;
/**
* Plugin implementation of the 'geofield_map' widget.
......@@ -75,6 +77,20 @@ class GeofieldMapWidget extends GeofieldLatLonWidget implements ContainerFactory
*/
protected $entityFieldManager;
/**
* The LeafletTileLayer Manager service.
*
* @var \Drupal\geofield_map\leafletTileLayer\LeafletTileLayerPluginManager
*/
protected $leafletTileManager;
/**
* The Current User.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Lat Lon widget components.
*
......@@ -90,64 +106,7 @@ class GeofieldMapWidget extends GeofieldLatLonWidget implements ContainerFactory
*
* @var array
*/
protected $leafletTileLayers = [
'OpenStreetMap_Mapnik' => [
'label' => 'OpenStreetMap Mapnik',
'url' => 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
'options' => [
'maxZoom' => 19,
'attribution' => '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
],
],
'OpenTopoMap' => [
'label' => 'OpenTopoMap',
'url' => 'http://{s}.tile.opentopomap.org/{z}/{x}/{y}.png',
'options' => [
'maxZoom' => 17,
'attribution' => 'Map data: &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: &copy; <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)',
],
],
'OpenMapSurfer_Roads' => [
'label' => 'OpenMapSurfer Roads',
'url' => 'http://korona.geog.uni-heidelberg.de/tiles/roads/x={x}&y={y}&z={z}',
'options' => [
'maxZoom' => 20,
'attribution' => 'Imagery from <a href="http://giscience.uni-hd.de/">GIScience Research Group @ University of Heidelberg</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
],
],
'Stamen_Toner' => [
'label' => 'Stamen Toner',
'url' => 'http://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}.{ext}',
'options' => [
'minZoom' => 0,
'maxZoom' => 20,
'ext' => 'png',
'attribution' => 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
],
],
'Stamen_Watercolor' => [
'label' => 'Stamen Watercolor',
'url' => 'http://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}',
'options' => [
'minZoom' => 1,
'maxZoom' => 16,
'ext' => 'png',
'subdomains' => 'abcd',
'attribution' => 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
],
],
'Stamen_Terrain' => [
'label' => 'Stamen Terrain',
'url' => 'http://stamen-tiles-{s}.a.ssl.fastly.net/terrain/{z}/{x}/{y}.{ext}',
'options' => [
'minZoom' => 4,
'maxZoom' => 18,
'ext' => 'png',
'bounds' => [[22, -132], [70, -56]],
'attribution' => 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
],
],
];
protected $leafletTileLayers;
/**
* Leaflet Map Tile Layers Options.
......@@ -183,6 +142,10 @@ class GeofieldMapWidget extends GeofieldLatLonWidget implements ContainerFactory
* The Entity Field Manager.
* @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
* The Link Generator service.
* @param \Drupal\geofield_map\leafletTileLayer\LeafletTileLayerPluginManager $leaflet_tile_manager
* The LeafletTileLayer Manager service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The Current User.
*/
public function __construct(
$plugin_id,
......@@ -196,7 +159,9 @@ class GeofieldMapWidget extends GeofieldLatLonWidget implements ContainerFactory
TranslationInterface $string_translation,
RendererInterface $renderer,
EntityFieldManagerInterface $entity_field_manager,
LinkGeneratorInterface $link_generator
LinkGeneratorInterface $link_generator,
LeafletTileLayerPluginManager $leaflet_tile_manager,
AccountInterface $current_user
) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $third_party_settings, $geophp_wrapper, $wkt_generator);
$this->config = $config_factory;
......@@ -204,11 +169,9 @@ class GeofieldMapWidget extends GeofieldLatLonWidget implements ContainerFactory
$this->entityFieldManager = $entity_field_manager;
$this->link = $link_generator;
$this->wktGenerator = $wkt_generator;
foreach ($this->leafletTileLayers as $k => $tile_layer) {
$this->leafletTileLayersOptions[$k] = $tile_layer['label'];
}
$this->leafletTileManager = $leaflet_tile_manager;
$this->leafletTileLayers = $this->leafletTileManager->getLeafletTileLayers();
$this->currentUser = $current_user;
}
/**
......@@ -227,7 +190,9 @@ class GeofieldMapWidget extends GeofieldLatLonWidget implements ContainerFactory
$container->get('string_translation'),
$container->get('renderer'),
$container->get('entity_field.manager'),
$container->get('link_generator')
$container->get('link_generator'),
$container->get('plugin.manager.leaflet_tile_layer_plugin'),
$container->get('current_user')
);
}
......@@ -350,7 +315,8 @@ class GeofieldMapWidget extends GeofieldLatLonWidget implements ContainerFactory
'#type' => 'select',
'#title' => $this->t('Map type'),
'#default_value' => $this->getSetting('map_type_leaflet'),
'#options' => $this->leafletTileLayersOptions,
'#options' => $this->leafletTileManager->getLeafletTilesLayersOptions(),
'#description' => $this->currentUser->hasPermission('configure geofield_map') ? $this->t('Choose one among all the Leaflet Tiles Plugins defined for the Geofield Map module (@see LeafletTileLayerPlugin).<br>You can add your one into your custom module as a new LeafletTileLayer Plugin. (Free Leaflet Tile Layers definitions are available from <a href="@free_leaflet_tiles_link" target="_blank">this link.</a>)', ['@free_leaflet_tiles_link' => 'http://leaflet-extras.github.io/leaflet-providers/preview/index.html']) : '',
'#states' => [
'visible' => [
':input[name="fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings][map_library]"]' => ['value' => 'leaflet'],
......
<?php
namespace Drupal\geofield_map\leafletTileLayerPlugin;
use Drupal\geofield_map\leafletTileLayers\LeafletTileLayerPluginBase;
/**
* Provides an Esri_WorldStreetMap Leaflet TileLayer Plugin.
*
* @LeafletTileLayerPlugin(
* id = "Esri_WorldStreetMap",
* label = "Esri WorldStreetMap",
* url = "https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}",
* options = {
* "attribution" = "Tiles &copy; Esri &mdash; Source: Esri, DeLorme, NAVTEQ, USGS, Intermap, iPC, NRCAN, Esri Japan, METI, Esri China (Hong Kong), Esri (Thailand), TomTom, 2012",
* }
* )
*/
class Esri_WorldStreetMap extends LeafletTileLayerPluginBase {}
<?php
namespace Drupal\geofield_map\leafletTileLayerPlugin;
use Drupal\geofield_map\leafletTileLayers\LeafletTileLayerPluginBase;
/**
* Provides an Esri_WorldTopoMap Leaflet TileLayer Plugin.
*
* @LeafletTileLayerPlugin(
* id = "Esri_WorldTopoMap",
* label = "Esri WorldTopoMap",
* url = "https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}",
* options = {
* "attribution" = "Tiles &copy; Esri &mdash; Esri, DeLorme, NAVTEQ, TomTom, Intermap, iPC, USGS, FAO, NPS, NRCAN, GeoBase, Kadaster NL, Ordnance Survey, Esri Japan, METI, Esri China (Hong Kong), and the GIS User Community",
* }
* )
*/
class Esri_WorldTopoMap extends LeafletTileLayerPluginBase {}
<?php
namespace Drupal\geofield_map\leafletTileLayerPlugin;
use Drupal\geofield_map\leafletTileLayers\LeafletTileLayerPluginBase;
/**
* Provides an OpenMapSurfer_Roads Leaflet TileLayer Plugin.
*
* @LeafletTileLayerPlugin(
* id = "OpenMapSurfer_Roads",
* label = "OpenMapSurfer Roads",
* url = "http://korona.geog.uni-heidelberg.de/tiles/roads/x={x}&y={y}&z={z}",
* options = {
* "maxZoom" = 20,
* "attribution" = "Imagery from <a href='http://giscience.uni-hd.de/'>GIScience Research Group @ University of Heidelberg</a> &mdash; Map data &copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>",
* }
* )
*/
class OpenMapSurfer_Roads extends LeafletTileLayerPluginBase {}
<?php
namespace Drupal\geofield_map\leafletTileLayerPlugin;
use Drupal\geofield_map\leafletTileLayers\LeafletTileLayerPluginBase;
/**
* Provides an OpenStreetMap_BlackAndWhite Leaflet TileLayer Plugin.
*
* @LeafletTileLayerPlugin(
* id = "OpenStreetMap_BlackAndWhite",
* label = "OpenStreetMap BlackAndWhite",
* url = "http://{s}.tiles.wmflabs.org/bw-mapnik/{z}/{x}/{y}.png",
* options = {
* "maxZoom" = 18,
* "attribution" = "&copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>",
* }
* )
*/
class OpenStreetMap_BlackAndWhite extends LeafletTileLayerPluginBase {}
<?php
namespace Drupal\geofield_map\leafletTileLayerPlugin;
use Drupal\geofield_map\leafletTileLayers\LeafletTileLayerPluginBase;
/**
* Provides an OpenStreetMap_Mapnik Leaflet TileLayer Plugin.
*
* @LeafletTileLayerPlugin(
* id = "OpenStreetMap_Mapnik",
* label = "OpenStreetMap Mapnik",
* url = "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
* options = {
* "maxZoom" = 19,
* "attribution" = "&copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>"
* }
* )
*/
class OpenStreetMap_Mapnik extends LeafletTileLayerPluginBase {}
<?php
namespace Drupal\geofield_map\leafletTileLayerPlugin;
use Drupal\geofield_map\leafletTileLayers\LeafletTileLayerPluginBase;
/**
* Provides an OpenTopoMap Leaflet TileLayer Plugin.
*
* @LeafletTileLayerPlugin(
* id = "OpenTopoMap",
* label = "OpenTopoMap",
* url = "http://{s}.tile.opentopomap.org/{z}/{x}/{y}.png",
* options = {
* "maxZoom" = 17,
* "attribution" = "Map data: &copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>, <a href='http://viewfinderpanoramas.org'>SRTM</a> | Map style: &copy; <a href='https://opentopomap.org'>OpenTopoMap</a> (<a href='https://creativecommons.org/licenses/by-sa/3.0/'>CC-BY-SA</a>)",
* }
* )
*/
class OpenTopoMap extends LeafletTileLayerPluginBase {}
<?php
namespace Drupal\geofield_map\leafletTileLayerPlugin;
use Drupal\geofield_map\leafletTileLayers\LeafletTileLayerPluginBase;
/**
* Provides an Stamen_Terrain Leaflet TileLayer Plugin.
*
* @LeafletTileLayerPlugin(
* id = "Stamen_Terrain",
* label = "Stamen Terrain",
* url = "http://stamen-tiles-{s}.a.ssl.fastly.net/terrain/{z}/{x}/{y}.{ext}",
* options = {
* "minZoom" = 0,
* "maxZoom" = 18,
* "ext" = "png",
* "subdomains" = "abcd",
* "attribution" = "Map tiles by <a href='http://stamen.com'>Stamen Design</a>, <a href='http://creativecommons.org/licenses/by/3.0'>CC BY 3.0</a> &mdash; Map data &copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>",
* }
* )
*/
class Stamen_Terrain extends LeafletTileLayerPluginBase {}
<?php
namespace Drupal\geofield_map\leafletTileLayerPlugin;
use Drupal\geofield_map\leafletTileLayers\LeafletTileLayerPluginBase;
/**
* Provides an Stamen_Toner Leaflet TileLayer Plugin.
*
* @LeafletTileLayerPlugin(
* id = "Stamen_Toner",
* label = "Stamen Toner",
* url = "http://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}.{ext}",
* options = {
* "minZoom" = 0,
* "maxZoom" = 20,
* "ext" = "png",
* "subdomains" = "abcd",
* "attribution" = "Map tiles by <a href='http://stamen.com'>Stamen Design</a>, <a href='http://creativecommons.org/licenses/by/3.0'>CC BY 3.0</a> &mdash; Map data &copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>",
* }
* )
*/
class Stamen_Toner extends LeafletTileLayerPluginBase {}
<?php
namespace Drupal\geofield_map\leafletTileLayerPlugin;
use Drupal\geofield_map\leafletTileLayers\LeafletTileLayerPluginBase;
/**
* Provides an Stamen_Watercolor Leaflet TileLayer Plugin.
*
* @LeafletTileLayerPlugin(
* id = "Stamen_Watercolor",
* label = "Stamen Watercolor",
* url = "http://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}",
* options = {
* "minZoom" = 1,
* "maxZoom" = 16,
* "ext" = "png",
* "subdomains" = "abcd",
* "attribution" = "Map tiles by <a href='http://stamen.com'>Stamen Design</a>, <a href='http://creativecommons.org/licenses/by/3.0'>CC BY 3.0</a> &mdash; Map data &copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap</a>",
* }
* )
*/
class Stamen_Watercolor extends LeafletTileLayerPluginBase {}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment