View source
<?php
namespace Drupal\geolocation\Element;
use Drupal\Core\Render\Element\FormElement;
use Drupal\Core\Form\FormStateInterface;
class GeolocationInput extends FormElement {
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#default_value' => NULL,
'#process' => [
[
$class,
'processGeolocation',
],
[
$class,
'processGroup',
],
],
'#pre_render' => [
[
$class,
'preRenderGroup',
],
],
'#element_validate' => [
[
$class,
'validateGeolocation',
],
],
'#theme_wrappers' => [
'fieldset',
],
];
}
public static function processGeolocation(array &$element, FormStateInterface $form_state, array &$complete_form) {
$default_field_values = [
'lat' => '',
'lng' => '',
];
if ($element['#defaults_loaded'] && isset($element['#value']['lat']) && isset($element['#value']['lng'])) {
$default_field_values = [
'lat' => $element['#value']['lat'],
'lng' => $element['#value']['lng'],
];
}
elseif (!empty($element['#default_value']) && isset($element['#default_value']['lat']) && isset($element['#default_value']['lng'])) {
$default_field_values = [
'lat' => $element['#default_value']['lat'],
'lng' => $element['#default_value']['lng'],
];
}
$element['lat'] = [
'#type' => 'textfield',
'#title' => t('Latitude'),
'#default_value' => $default_field_values['lat'],
'#attributes' => [
'class' => [
'geolocation-input-latitude',
'geolocation-input-latitude',
],
],
];
$element['lng'] = [
'#type' => 'textfield',
'#title' => t('Longitude'),
'#default_value' => $default_field_values['lng'],
'#attributes' => [
'class' => [
'geolocation-input-longitude',
],
],
];
if (empty($element['#wrapper_attributes'])) {
$element['#wrapper_attributes'] = [];
}
$element['#wrapper_attributes'] = array_merge_recursive($element['#wrapper_attributes'], [
'class' => [
'geolocation-input',
],
]);
return $element;
}
public static function validateGeolocation(array &$element, FormStateInterface $form_state, array &$complete_form) {
if (empty($element['#value']['lng']) && empty($element['#value']['lat'])) {
return;
}
if (!is_numeric($element['#value']['lng'])) {
$form_state
->setError($element, t('Longitude not numeric.'));
}
if (!is_numeric($element['#value']['lat'])) {
$form_state
->setError($element, t('Latitude not numeric.'));
}
$longitude = floatval($element['#value']['lng']);
$latitude = floatval($element['#value']['lat']);
if ($latitude < -90 || $latitude > 90) {
$form_state
->setError($element, t('Latitude must be between -90 and 90.'));
}
if ($longitude < -180 || $longitude > 180) {
$form_state
->setError($element, t('Longitude must be between -180 and 180.'));
}
}
}