GeofieldDmsWidget.php in Geofield 8
File
src/Plugin/Field/FieldWidget/GeofieldDmsWidget.php
View source
<?php
namespace Drupal\geofield\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\geofield\DmsConverter;
use Drupal\geofield\DmsPoint;
class GeofieldDmsWidget extends GeofieldBaseWidget {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$latlon_value = [];
foreach ([
'lat',
'lon',
] as $component) {
$latlon_value[$component] = isset($items[$delta]->{$component}) ? floatval($items[$delta]->{$component}) : '';
}
$element += [
'#type' => 'geofield_dms',
'#default_value' => $latlon_value,
];
return [
'value' => $element,
];
}
public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
foreach ($values as $delta => $value) {
if (is_numeric($value['value']['lon']['degrees']) && is_numeric($value['value']['lon']['minutes']) && is_numeric($value['value']['lon']['seconds']) && is_numeric($value['value']['lat']['degrees']) && is_numeric($value['value']['lat']['minutes']) && is_numeric($value['value']['lat']['seconds'])) {
$components = DmsConverter::dmsToDecimal(new DmsPoint($value['value']['lon'], $value['value']['lat']));
$values[$delta]['value'] = $this
->geofieldBackendValue($this->wktGenerator
->wktGeneratePoint($components));
}
else {
$values[$delta]['value'] = NULL;
}
}
return $values;
}
}