Refactor MapKnitter module to use a farmOS-map behavior.

This commit is contained in:
Michael Stenta 2019-09-24 11:49:47 -04:00
parent d7fd542069
commit d67fb1c796
6 changed files with 55 additions and 156 deletions

View File

@ -0,0 +1,16 @@
(function () {
farmOS.map.behaviors.mapknitter = {
attach: function (instance) {
var slug = Drupal.settings.farm_map.behaviors.mapknitter.slug;
var title = Drupal.settings.farm_map.behaviors.mapknitter.title;
var opts = {
title: title,
url: 'https://mapknitter.org/tms/' + slug + '/{z}/{x}/{-y}.png',
group: 'MapKnitter',
visible: true,
};
instance.addLayer('xyz', opts);
},
weight: -101,
};
}());

View File

@ -1,55 +0,0 @@
<?php
/**
* @file
* farm_mapknitter.default_openlayers_layers.inc
*/
/**
* Implements hook_default_openlayers_layers().
*/
function farm_mapknitter_default_openlayers_layers() {
$export = array();
// Load the MapKnitter slug and title.
$slug = variable_get('farm_mapknitter_slug', '');
$title = variable_get('farm_mapknitter_title', '');
// If the slug or title is empty, bail.
if (empty($slug) || empty($title)) {
return $export;
}
// Add a MapKnitter layer.
$ol_layer = new stdClass();
$ol_layer->disabled = FALSE; /* Edit this to true to make a default ol_layer disabled initially */
$ol_layer->api_version = 1;
$ol_layer->machine_name = 'farm_mapknitter_layer';
$ol_layer->name = check_plain($title);
$ol_layer->description = '';
$ol_layer->factory_service = 'openlayers.Layer:Tile';
$ol_layer->options = array(
'source' => 'farm_mapknitter_source',
'visible' => 1,
);
$export['farm_mapknitter_layer'] = $ol_layer;
// Add a layer group for MapKnitter layers.
$ol_layer = new stdClass();
$ol_layer->disabled = FALSE; /* Edit this to true to make a default ol_layer disabled initially */
$ol_layer->api_version = 1;
$ol_layer->machine_name = 'farm_mapknitter_layer_group';
$ol_layer->name = 'Farm MapKnitter Layer Group';
$ol_layer->description = '';
$ol_layer->factory_service = 'openlayers.Layer:Group';
$ol_layer->options = array(
'source' => '',
'visible' => 1,
'grouptitle' => 'MapKnitter layers',
'grouplayers' => array(
'farm_mapknitter_layer',
),
);
$export['farm_mapknitter_layer_group'] = $ol_layer;
return $export;
}

View File

@ -1,37 +0,0 @@
<?php
/**
* @file
* farm_mapknitter.default_openlayers_maps.inc
*/
/**
* Implements hook_default_openlayers_maps_alter().
*/
function farm_mapknitter_default_openlayers_maps_alter(&$maps) {
// Load the MapKnitter slug.
$slug = variable_get('farm_mapknitter_slug', '');
// If the slug is blank, bail.
if (empty($slug)) {
return;
}
// Add the MapKnitter layer group to farm maps.
$farm_maps = array(
'farm_areas',
'farm_map_geofield',
'farm_map_geofield_formatter',
);
// Iterate over the farm maps and see if they exist.
foreach ($farm_maps as $farm_map) {
// If this map exists in the list of existing maps...
if (array_key_exists($farm_map, $maps)) {
// Add the MapKnitter layer group.
$maps[$farm_map]->options['layers'][] = 'farm_mapknitter_layer_group';
}
}
}

View File

@ -1,41 +0,0 @@
<?php
/**
* @file
* farm_mapknitter.default_openlayers_sources.inc
*/
/**
* Implements hook_default_openlayers_sources().
*/
function farm_mapknitter_default_openlayers_sources() {
$export = array();
// Load the MapKnitter slug.
$slug = variable_get('farm_mapknitter_slug', '');
// If the slug is blank, bail.
if (empty($slug)) {
return $export;
}
// Check plain.
$slug = check_plain($slug);
// Assemble the URL.
$url = 'https://mapknitter.org/tms/' . $slug . '/{z}/{x}/{-y}.png';
// Generate a MapKnitter source.
$ol_source = new stdClass();
$ol_source->disabled = FALSE; /* Edit this to true to make a default ol_source disabled initially */
$ol_source->api_version = 1;
$ol_source->machine_name = 'farm_mapknitter_source';
$ol_source->name = 'Farm MapKnitter Source';
$ol_source->description = '';
$ol_source->factory_service = 'openlayers.Source:XYZ';
$ol_source->options = array(
'url' => $url,
);
$export['farm_mapknitter_source'] = $ol_source;
return $export;
}

View File

@ -0,0 +1,39 @@
<?php
/**
* @file
* Farm Map hooks implemented by the Farm Mapknitter module.
*/
/**
* Implements hook_farm_map_behaviors().
*/
function farm_mapknitter_farm_map_behaviors() {
return array(
'mapknitter' => array(
'js' => 'farmOS.map.behaviors.mapknitter.js',
),
);
}
/**
* Implements hook_farm_map_behavior_settings().
*/
function farm_mapknitter_farm_map_behavior_settings($behavior) {
$settings = array();
if ($behavior == 'mapknitter') {
$settings['slug'] = variable_get('farm_mapknitter_slug', '');
$settings['title'] = variable_get('farm_mapknitter_title', '');
}
return $settings;
}
/**
* Implements hook_farm_map_view().
*/
function farm_mapknitter_farm_map_view($name, $element) {
// If a MapKnitter slug and title are set, add the layer to all farmOS maps.
if (variable_get('farm_mapknitter_slug', FALSE) && variable_get('farm_mapknitter_title', FALSE)) {
farm_map_add_behavior('mapknitter');
}
}

View File

@ -71,29 +71,6 @@ function farm_mapknitter_form($form, &$form_state) {
'#default_value' => variable_get('farm_mapknitter_slug', ''),
);
$form['#submit'][] = 'farm_mapknitter_form_submit';
return system_settings_form($form);
}
/**
* Farm MapKnitter form submit function.
*/
function farm_mapknitter_form_submit($form, &$form_state) {
drupal_flush_all_caches();
}
/**
* Implements hook_ctools_plugin_api().
*/
function farm_mapknitter_ctools_plugin_api($module = NULL, $api = NULL) {
if ($module == "openlayers" && $api == "default_openlayers_layers") {
return array("version" => "1");
}
if ($module == "openlayers" && $api == "default_openlayers_maps") {
return array("version" => "1");
}
if ($module == "openlayers" && $api == "default_openlayers_sources") {
return array("version" => "1");
}
}