From e70b092c0590dccb1914fbe092b653a571469d05 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Sat, 18 Jun 2022 07:54:59 -0400 Subject: [PATCH] Return a string value from farm_map_input elements instead of an array. --- modules/core/map/src/Element/FarmMapInput.php | 15 +++++---------- .../Plugin/Field/FieldWidget/GeofieldWidget.php | 14 ++++++++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/modules/core/map/src/Element/FarmMapInput.php b/modules/core/map/src/Element/FarmMapInput.php index dfa88b405..a8faf5d61 100644 --- a/modules/core/map/src/Element/FarmMapInput.php +++ b/modules/core/map/src/Element/FarmMapInput.php @@ -20,8 +20,6 @@ class FarmMapInput extends FormElement { $class = static::class; return [ '#input' => TRUE, - // @todo Does this have to return a tree structure? - '#tree' => TRUE, '#process' => [ [$class, 'processElement'], ], @@ -59,6 +57,7 @@ class FarmMapInput extends FormElement { * The processed element. */ public static function processElement(array $element, FormStateInterface $form_state, array &$complete_form) { + $element['#tree'] = TRUE; // Merge provided map behaviors into defaults. Enable wkt and input // behaviors if #disabled is not TRUE. @@ -76,8 +75,6 @@ class FarmMapInput extends FormElement { ], $element['#map_settings']); // Define the map render array. - // @todo Does this have to return a tree structure? - $element['#tree'] = TRUE; $element['map'] = [ '#type' => 'farm_map', '#map_type' => $element['#map_type'], @@ -134,6 +131,9 @@ class FarmMapInput extends FormElement { $form_state->setError($element, t('"@value" is not a valid geospatial content.', ['@value' => $value])); } } + + // Save the WKT string value to the overall element. + $form_state->setValueForElement($element, $value); } /** @@ -143,12 +143,7 @@ class FarmMapInput extends FormElement { if ($input === FALSE) { return $element['#default_value'] ?: ''; } - - if ($input['value']) { - return $input['value']; - } - - return NULL; + return $input; } } diff --git a/modules/core/map/src/Plugin/Field/FieldWidget/GeofieldWidget.php b/modules/core/map/src/Plugin/Field/FieldWidget/GeofieldWidget.php index dd158b075..dfab211c8 100644 --- a/modules/core/map/src/Plugin/Field/FieldWidget/GeofieldWidget.php +++ b/modules/core/map/src/Plugin/Field/FieldWidget/GeofieldWidget.php @@ -149,9 +149,9 @@ class GeofieldWidget extends GeofieldBaseWidget { $element['#suffix'] = ''; // Get the current form state value. Prioritize form state over field value. - $form_value = $form_state->getValue([$field_name, $delta, 'value']); + $form_value = $form_state->getValue([$field_name, $delta]); $field_value = $items[$delta]->value; - $current_value = $form_value ?? $field_value; + $current_value = $form_value['value'] ?? $field_value; $element['#default_value'] = $current_value; // Configure to display raw geometry. @@ -181,6 +181,12 @@ class GeofieldWidget extends GeofieldBaseWidget { ]; } + // Override the element validation to prevent transformation of the value + // from array to string, and because Geofields already perform the same + // geometry validation. + // @see \Drupal\geofield\Plugin\Validation\GeoConstraintValidator. + $element['#element_validate'] = []; + return $element; } @@ -267,11 +273,11 @@ class GeofieldWidget extends GeofieldBaseWidget { $field_name = $this->fieldDefinition->getName(); $delta = $element['#delta']; $user_input = $form_state->getUserInput(); - unset($user_input[$field_name][$delta]['value']); + unset($user_input[$field_name][$delta]); $form_state->setUserInput($user_input); // Set the new form value. - $form_state->setValue([$field_name, $delta, 'value'], $wkt); + $form_state->setValue([$field_name, $delta], ['value' => $wkt]); // Rebuild the form so the map widget is rebuilt with the new value. $form_state->setRebuild(TRUE);