visualn-8.x-1.x-dev/modules/visualn_basic_drawers/src/Plugin/VisualN/Drawer/LeafletMapBasicDrawer.php
modules/visualn_basic_drawers/src/Plugin/VisualN/Drawer/LeafletMapBasicDrawer.php
<?php
namespace Drupal\visualn_basic_drawers\Plugin\VisualN\Drawer;
use Drupal\visualn\Core\DrawerWithJsBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\visualn\ResourceInterface;
/**
* Provides a 'Leaflet Map Basic' VisualN drawer.
*
* @ingroup drawer_plugins
*
* @VisualNDrawer(
* id = "visualn_leaflet_map_basic",
* label = @Translation("Leaflet Map Basic"),
* )
*/
class LeafletMapBasicDrawer extends DrawerWithJsBase {
/**
* {@inheritdoc}
*/
public function getDescription() {
return t('Openstreetmap based leaflet map with geotags support');
}
/**
* @inheritdoc
*/
public function defaultConfiguration() {
$default_config = [
'center_lat' => -27.11667,
'center_lng' => -109.35000,
'map_height' => '',
'calculate_center' => 1,
];
return $default_config;
}
// @todo: remove whitespaces on submit, and validate to allow only
// valid coordinates values
/**
* @inheritdoc
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
// get calculate_center #name property for #states settings
$element_path = isset($form['#parents']) ? $form['#parents'] : [];
$element_path[] = 'calculate_center';
$name = array_shift($element_path);
if (!empty($element_path)) {
$name .= '[' . implode('][', $element_path) . ']';
}
$form['center_lat'] = [
'#type' => 'textfield',
'#title' => t('Center latitude'),
'#default_value' => $this->configuration['center_lat'],
'#required' => TRUE,
'#size' => 10,
// @todo: actually if no data provided, should fall back to center coords
/*
'#states' => [
'disabled' => [
':input[name="' . $name . '"]' => ['value' => 1],
],
],
*/
];
$form['center_lng'] = [
'#type' => 'textfield',
'#title' => t('Center longitude'),
'#default_value' => $this->configuration['center_lng'],
'#required' => TRUE,
'#size' => 10,
/*
'#states' => [
'disabled' => [
':input[name="' . $name . '"]' => ['value' => 1],
],
],
*/
];
$form['calculate_center'] = [
// use radios instead of checkbox to avoid the bug with ajax
// see https://www.drupal.org/project/drupal/issues/1988968
'#type' => 'radios',
'#options' => [1 => t('yes'), 0 => t('no')],
'#title' => t('Calculate center'),
'#default_value' => $this->configuration['calculate_center'],
'#description' => t('Get center based on points data. If no points provided, center latitude and longitude are used.'),
];
$form['map_height'] = [
'#type' => 'number',
'#title' => t('Map height (px)'),
'#default_value' => $this->configuration['map_height'],
'#attributes' => ['placeholder' => '350'],
'#min' => 1,
];
return $form;
}
/**
* @inheritdoc
*/
public function prepareBuild(array &$build, $vuid, ResourceInterface $resource) {
// check drawing window parameters
$window_parameters = $this->getWindowParameters();
if (!empty($window_parameters['height'])) {
$this->configuration['map_height'] = $window_parameters['height'];
}
// Attach drawer config to js settings
parent::prepareBuild($build, $vuid, $resource);
// @todo: $resource = parent::prepareBuild($build, $vuid, $resource); (?)
// Attach visualn style libraries
$build['#attached']['library'][] = 'visualn_basic_drawers/leaflet-map-basic-drawer';
return $resource;
}
/**
* @inheritdoc
*/
public function jsId() {
return 'visualnLeafletMapBasicDrawer';
}
/**
* @inheritdoc
*/
public function prepareJsConfig(array &$drawer_config) {
$drawer_config['calculate_center'] = (integer) $drawer_config['calculate_center'];
$drawer_config['protocol'] = \Drupal::request()->isSecure() ? 'https' : 'http';
}
/**
* @inheritdoc
*/
public function dataKeys() {
$data_keys = [
'title',
'lng',
'lat',
];
return $data_keys;
}
}
