Add farm_map_wkt form element.

This commit is contained in:
Paul Weidner 2022-06-14 18:20:36 -07:00 committed by Michael Stenta
parent e6f1c85c8f
commit 17dd03f23c
1 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,110 @@
<?php
namespace Drupal\farm_map\Element;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\FormElement;
/**
* Form element that returns WKT rendered in a map.
*
* @FormElement("farm_map_wkt")
*/
class FarmMapWkt extends FormElement {
/**
* {@inheritdoc}
*/
public function getInfo() {
$class = static::class;
return [
'#input' => TRUE,
// @todo Does this have to return a tree structure?
'#tree' => TRUE,
'#process' => [
[$class, 'processElement'],
],
'#pre_render' => [
[$class, 'preRenderGroup'],
],
// @todo Add validation.
// '#element_validate' => [
// [$class, 'elementValidate'],
// ],
'#theme_wrappers' => ['fieldset'],
// Display descriptions above the map by default.
'#description_display' => 'before',
'#map_type' => 'geofield_widget',
'#display_raw_geometry' => TRUE,
];
}
/**
* Generates the FarmMapWktform element.
*
* @param array $element
* An associative array containing the properties and children of the
* element. Note that $element must be taken by reference here, so processed
* child elements are taken over into $form_state.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*
* @return array
* The processed element.
*/
public static function processElement(array $element, FormStateInterface $form_state, array &$complete_form) {
// 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'],
'#map_settings' => [
'behaviors' => [
'wkt' => [
'edit' => TRUE,
'zoom' => TRUE,
],
],
],
];
// Add a textarea for the WKT value.
$display_raw_geometry = $element['#display_raw_geometry'];
$element['value'] = [
'#type' => $display_raw_geometry ? 'textarea' : 'hidden',
'#title' => t('Geometry'),
'#attributes' => [
'data-map-geometry-field' => TRUE,
],
];
// Add default value if provided.
if (!empty($element['#default_value'])) {
$element['map']['#map_settings']['wkt'] = $element['#default_value'];
$element['value']['#default_value'] = $element['#default_value'];
}
// Return the element.
return $element;
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input === FALSE) {
return $element['#default_value'] ?: [];
}
if ($input['value']) {
return $input['value'];
}
return NULL;
}
}