Update GeofieldWidget to use farm_map_wkt.

This commit is contained in:
Paul Weidner 2022-06-14 18:22:45 -07:00 committed by Michael Stenta
parent 17dd03f23c
commit 6e6c96eeef
1 changed files with 8 additions and 28 deletions

View File

@ -133,12 +133,12 @@ class GeofieldWidget extends GeofieldBaseWidget {
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
// Wrap the map in a collapsible details element.
// Use the farm_map_wkt form element.
$element['#type'] = 'farm_map_wkt';
// Wrap the map with a unique id for populating from files.
$field_name = $this->fieldDefinition->getName();
$field_wrapper_id = Html::getUniqueId($field_name . '_wrapper');
$element['#type'] = 'details';
$element['#title'] = $this->t('Geometry');
$element['#open'] = TRUE;
$element['#prefix'] = '<div id="' . $field_wrapper_id . '">';
$element['#suffix'] = '</div>';
@ -146,32 +146,11 @@ class GeofieldWidget extends GeofieldBaseWidget {
$form_value = $form_state->getValue([$field_name, $delta, 'value']);
$field_value = $items[$delta]->value;
$current_value = $form_value ?? $field_value;
$element['#default_value'] = $current_value;
// Define the map render array.
$element['map'] = [
'#type' => 'farm_map',
'#map_type' => 'geofield_widget',
'#map_settings' => [
'wkt' => $current_value,
'behaviors' => [
'wkt' => [
'edit' => TRUE,
'zoom' => TRUE,
],
],
],
];
// Add a textarea for the WKT value.
// Configure to display raw geometry.
$display_raw_geometry = $this->getSetting('display_raw_geometry');
$element['value'] = [
'#type' => $display_raw_geometry ? 'textarea' : 'hidden',
'#title' => $this->t('Geometry'),
'#default_value' => $current_value,
'#attributes' => [
'data-map-geometry-field' => TRUE,
],
];
$element['#display_raw_geometry'] = $display_raw_geometry;
// Add an option to populate geometry using files field.
// The "populate_file_field" field setting must be configured and the
@ -192,6 +171,7 @@ class GeofieldWidget extends GeofieldBaseWidget {
':input[name="' . $populate_file_field . '[0][fids]"]' => ['empty' => TRUE],
],
],
'#weight' => 10,
];
}