GeofieldLatLonWidget.php in Geofield 8
File
src/Plugin/Field/FieldWidget/GeofieldLatLonWidget.php
View source
<?php
namespace Drupal\geofield\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
class GeofieldLatLonWidget extends GeofieldBaseWidget {
public $components = [
'lon',
'lat',
];
public static function defaultSettings() {
return [
'html5_geolocation' => FALSE,
] + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$elements = parent::settingsForm($form, $form_state);
$elements['html5_geolocation'] = [
'#type' => 'checkbox',
'#title' => 'Use HTML5 Geolocation to set default values',
'#default_value' => $this
->getSetting('html5_geolocation'),
];
return $elements;
}
public function settingsSummary() {
return [
$this
->t('HTML5 Geolocation button is @state', [
'@state' => $this
->getSetting('html5_geolocation') ? $this
->t('enabled') : $this
->t('disabled'),
]),
];
}
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$latlon_value = [];
foreach ($this->components as $component) {
$latlon_value[$component] = isset($items[$delta]->{$component}) ? floatval($items[$delta]->{$component}) : '';
}
$element += [
'#type' => 'geofield_latlon',
'#default_value' => $latlon_value,
'#geolocation' => $this
->getSetting('html5_geolocation'),
'#error_label' => !empty($element['#title']) ? $element['#title'] : $this->fieldDefinition
->getLabel(),
];
return [
'value' => $element,
];
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as $delta => $value) {
foreach ($this->components as $component) {
if (empty($value['value'][$component]) || !is_numeric($value['value'][$component])) {
$values[$delta]['value'] = '';
continue 2;
}
}
$components = $value['value'];
$values[$delta]['value'] = $this
->geofieldBackendValue($this->wktGenerator
->wktBuildPoint([
trim($components['lon']),
trim($components['lat']),
]));
}
return $values;
}
}