openlayers-8.x-4.x-dev/src/MapSettings.php
src/MapSettings.php
<?php
namespace Drupal\openlayers;
/**
* Generates the complete settings for the selected map, together with all the related layers / sources / interactions / etc.
*/
class MapSettings {
/**
* The settings for the selected map. These settings will be passed to the javascript file that generates the map.
*
* @var array
*/
public $settings;
public function __construct($map_id) {
$this->settings = $this->getSettings($map_id, 'map');
}
/**
* {@inheritdoc}
*/
public function getSettings($id, $type) {
$id = 'openlayers.' . $type . '.' . $id;
$config = \Drupal::config($id)->get();
if ($type == 'map') {
$this->cacheTags = ['config:' . $id];
}
$settings = [];
// TODO
if ($type == 'map') {
$settings['map'] = [
'mn' => 'openlayers_geofield_map_geofield_formatter',
'fs' => 'openlayers.Map:OLMap',
'opt' => []
];
}
foreach ($config as $key => $item) {
if (in_array($key, ['uuid', 'langcode', 'status', 'dependencies', '_core', 'id', 'label', 'is_configurable'])) {
continue;
}
switch ($key) {
case 'controls':
case 'layers':
case 'styles':
case 'interactions':
if (count($item) > 0) {
$layers = [];
foreach ($item as $layer_item) {
$layer = $this->getSettings($layer_item['id'], 'layer');
foreach($layer_item['data'] as $key_data => $item_data) {
if ($key_data == 'factory_service') {
$layer['fs'] = $item_data;
} else {
$layer['opt'][$key_data] = $item_data;
}
}
$settings[$key . 'XX'][] = $layer;
}
}
break;
default:
$settings[$key] = $item;
}
}
return $settings;
}
}