View source
<?php
namespace Drupal\geolocation_leaflet\Plugin\geolocation\MapFeature;
use Drupal\Core\Render\BubbleableMetadata;
class LeafletControlScale extends ControlElementBase {
public static function getDefaultSettings() {
return [
'metric' => TRUE,
'imperial' => TRUE,
];
}
public function getSettingsForm(array $settings, array $parents) {
$form = parent::getSettingsForm($settings, $parents);
$settings = $this
->getSettings($settings);
$form['metric'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Metric'),
'#description' => $this
->t('Whether to show the metric scale line (m/km).'),
'#default_value' => $settings['metric'],
];
$form['imperial'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Imperial'),
'#description' => $this
->t('Whether to show the imperial scale line (mi/ft).'),
'#default_value' => $settings['imperial'],
];
return $form;
}
public function alterMap(array $render_array, array $feature_settings, array $context = []) {
$render_array = parent::alterMap($render_array, $feature_settings, $context);
$feature_settings = $this
->getSettings($feature_settings);
$render_array['#attached'] = BubbleableMetadata::mergeAttachments(empty($render_array['#attached']) ? [] : $render_array['#attached'], [
'drupalSettings' => [
'geolocation' => [
'maps' => [
$render_array['#id'] => [
$this
->getPluginId() => [
'metric' => $feature_settings['metric'],
'imperial' => $feature_settings['imperial'],
],
],
],
],
],
]);
return $render_array;
}
}