Return a string value from farm_map_input elements instead of an array.

This commit is contained in:
Michael Stenta 2022-06-18 07:54:59 -04:00
parent de94168269
commit e70b092c05
2 changed files with 15 additions and 14 deletions

View File

@ -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;
}
}

View File

@ -149,9 +149,9 @@ class GeofieldWidget extends GeofieldBaseWidget {
$element['#suffix'] = '</div>';
// 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);