openlayers-8.x-4.x-dev/src/Plugin/Map/OLMap/OLMap.php
src/Plugin/Map/OLMap/OLMap.php
<?php
namespace Drupal\openlayers\Plugin\Map\OLMap;
use Drupal\Core\Form\FormStateInterface;
use Drupal\openlayers\Types\Map;
/**
* Defines the Map object for an Openlayers map.
*
* @OpenlayersPlugin(
* id = "OLMap",
* label = @Translation("Openlayers Map"),
* description = @Translation("An openlayers map."),
* service = "openlayers.Map:OLMap",
* library = "openlayers-plugin-map-olmap",
* type = "map"
* )
*/
class OLMap extends Map {
/**
* {@inheritdoc}
*/
public function getSummary() {
$summary = [
'#theme' => 'openlayers_map_summary',
'#data' => $this->configuration,
];
$summary += parent::getSummary();
return $summary;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() { // TODO - this appears to override config provided when using createInstance of plugin
return [
'className' => 'ol-map',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['className'] = [
'#type' => 'textfield',
'#default_value' => $this->configuration['className'],
'#title' => t('Class name'),
'#description' => t('CSS class name.'),
'#size' => 50,
'#maxlength' => 50,
'#required' => FALSE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['className'] = $form_state->getValue('className');
}
public function attach_js_settings($map_id) {
$js_settings = [];
// TODO - determine plugin types rather than use hard-coded array
$pluginTypes = ['sources', 'styles', 'interactions', 'controls', 'components', 'layers'];
foreach ($pluginTypes as $pluginType) {
if (!empty($mapConfig[$pluginType])) {
$configObjects = $mapConfig[$pluginType];
$pluginObjects = [];
foreach ($configObjects as $key => $item) {
if (!empty($item['plugin_definition']['library'])) {
$js = array(
'mn' => $item['id'],
'fs' => $item['plugin_definition']['service'],
);
if (!empty($item['data'])) {
$js['opt'] = $item['data'];
}
$pluginObjects[] = $js;
} else {
\Drupal::messenger()->addError('Plugin "' . $item['id'] . '" does not have a js library defined.');
\Drupal::logger('openlayers')->error('Plugin "' . $item['id'] . '" does not have a js library defined.');
}
}
$js_settings[substr($pluginType, 0, -1)] = $pluginObjects;
}
}
$js_settings['map'] = [
'opt' => [
'view' => [
'center' => [
'lon' => 0,
'lat' => 51,
],
'rotation' => 0,
],
],
];
}
}
