View source
<?php
namespace Drupal\search_api_location\Plugin\search_api_location\location_input;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search_api_location\LocationInput\LocationInputPluginBase;
class Map extends LocationInputPluginBase {
public function hasInput(array $input, array $options) {
return $input['lat'] && $input['lng'];
}
public function getParsedInput(array $input) {
if (!isset($input['lat']) || !isset($input['lng'])) {
throw new \InvalidArgumentException('Input doesn\'t contain a location value.');
}
return $input['lat'] . ',' . $input['lng'];
}
public function defaultConfiguration() {
$configuration = parent::defaultConfiguration();
$configuration += [
'radius_border_color' => '',
'radius_border_weight' => '',
'radius_background_color' => '',
'radius_background_transparency' => '',
'marker_image' => '',
];
return $configuration;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['radius_border_color'] = [
'#type' => 'textfield',
'#title' => $this
->t('Border color'),
'#description' => $this
->t('The hexadecimal value of the radius border color.'),
'#default_value' => $this->configuration['radius_border_color'],
'#size' => 7,
];
$form['radius_border_weight'] = [
'#type' => 'textfield',
'#title' => $this
->t('Border weight'),
'#description' => $this
->t('The radius border weight in pixels.'),
'#default_value' => $this->configuration['radius_border_weight'],
'#size' => 3,
];
$form['radius_background_color'] = [
'#type' => 'textfield',
'#title' => $this
->t('Fill color'),
'#description' => $this
->t('The hexadecimal value of the fill color.'),
'#default_value' => $this->configuration['radius_background_color'],
'#size' => 7,
];
$form['radius_background_transparency'] = [
'#type' => 'textfield',
'#title' => $this
->t('Fill transparency'),
'#description' => $this
->t('The opacity of the fill color (a value between 0.0 and 1.0)'),
'#default_value' => $this->configuration['radius_background_transparency'],
'#size' => 3,
];
$form['marker_image'] = [
'#type' => 'textfield',
'#title' => $this
->t('Marker image'),
'#description' => $this
->t('The path to the marker image.'),
'#default_value' => $this->configuration['marker_image'],
];
return $form;
}
public function getForm(array $form, FormStateInterface $form_state, array $options) {
$form['value']['#tree'] = TRUE;
$lat_default_value = (double) $form_state
->getUserInput()[$options['expose']['identifier']]['lat'] ?: "51.037932";
$lng_default_value = (double) $form_state
->getUserInput()[$options['expose']['identifier']]['lng'] ?: "3.712105";
$radius_default_value = (double) $form_state
->getUserInput()[$options['expose']['identifier']]['distance']['from'] ?: "1000";
$id = $options['id'];
$form['value']['lat'] = [
'#type' => 'hidden',
'#default_value' => $lat_default_value,
'#attributes' => [
'id' => [
"sal-{$id}-lat",
],
],
];
$form['value']['lng'] = [
'#type' => 'hidden',
'#default_value' => $lng_default_value,
'#attributes' => [
'id' => [
"sal-{$id}-lng",
],
],
];
$form['value']['distance']['from'] = [
'#type' => 'hidden',
'#default_value' => $radius_default_value,
'#attributes' => [
'id' => [
"sal-{$id}-radius",
],
],
];
$form['value']['map'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'id' => "sal-{$id}-map",
'class' => "sal-map",
],
'#attached' => [
'library' => [
'search_api_location/location-picker',
],
'drupalSettings' => [
'search_api_location' => [
$id => [
'id' => $id,
'lat' => (double) $lat_default_value,
'lng' => (double) $lng_default_value,
'radius' => $radius_default_value,
'radius_border_color' => $options['plugin-geocode_map']['radius_border_color'],
'radius_border_weight' => $options['plugin-geocode_map']['radius_border_weight'],
'radius_background_color' => $options['plugin-geocode_map']['radius_background_color'],
'radius_background_transparency' => $options['plugin-geocode_map']['radius_background_transparency'],
'marker_image' => $options['plugin-geocode_map']['marker_image'],
],
],
],
],
];
$form['value']['slider'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'id' => "sal-{$id}-slider",
'class' => "sal-slider",
],
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
}