Skip to content

Commit ef098cc

Browse files
committed
feat: save populated userform field data
1 parent 550d875 commit ef098cc

File tree

4 files changed

+170
-24
lines changed

4 files changed

+170
-24
lines changed

client/javascript/addressfinder.js

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
document.addEventListener("DOMContentLoaded", function () {
2+
const setValueOnElement = (element, value) => {
3+
if (element) {
4+
element.value = value;
5+
}
6+
};
7+
28
/**
39
* Sets up the Address Finder field.
410
* @param {HTMLElement} elem
@@ -20,6 +26,7 @@ document.addEventListener("DOMContentLoaded", function () {
2026

2127
if (address) {
2228
field = address.querySelector("input");
29+
address.style.display = "block";
2330
}
2431

2532
if (!field) {
@@ -36,10 +43,10 @@ document.addEventListener("DOMContentLoaded", function () {
3643
toggle.style.display = "block";
3744
}
3845

39-
address.style.display = "block";
40-
4146
if (useManual && useManual.value !== "1") {
42-
manual.style.display = "none";
47+
if (manual) {
48+
manual.style.display = "none";
49+
}
4350
}
4451

4552
// Create widget
@@ -49,28 +56,45 @@ document.addEventListener("DOMContentLoaded", function () {
4956

5057
// Update manual fields and hidden metadata
5158
widget.on("result:select", function (value, item) {
52-
for (var i = 1; i <= 6; i++) {
53-
var postalInput = manual.querySelector(
54-
"input[name*=PostalLine" + i + "]"
55-
);
56-
if (postalInput) {
57-
postalInput.value = item["postal_line_" + i] || "";
59+
if (manual) {
60+
for (var i = 1; i <= 6; i++) {
61+
var postalInput = manual.querySelector(
62+
"input[name*=PostalLine" + i + "]"
63+
);
64+
if (postalInput) {
65+
postalInput.value = item["postal_line_" + i] || "";
66+
}
5867
}
59-
}
6068

61-
if (manual) {
62-
manual.querySelector("input[name*=Suburb]").value =
63-
item.suburb || "";
64-
manual.querySelector("input[name*=Region]").value =
65-
item.region || "";
66-
manual.querySelector("input[name*=City]").value =
67-
item.city || "";
68-
manual.querySelector("input[name*=Postcode]").value =
69-
item.postcode || "";
70-
manual.querySelector("input[name*=Longitude]").value =
71-
item.x || "";
72-
manual.querySelector("input[name*=Latitude]").value =
73-
item.y || "";
69+
setValueOnElement(
70+
manual.querySelector("input[name*=Suburb]"),
71+
item.suburb || ""
72+
);
73+
74+
setValueOnElement(
75+
manual.querySelector("input[name*=Region]"),
76+
item.region || ""
77+
);
78+
79+
setValueOnElement(
80+
manual.querySelector("input[name*=City]"),
81+
item.city || ""
82+
);
83+
84+
setValueOnElement(
85+
manual.querySelector("input[name*=Postcode]"),
86+
item.postcode || ""
87+
);
88+
89+
setValueOnElement(
90+
manual.querySelector("input[name*=Longitude]"),
91+
item.x || ""
92+
);
93+
94+
setValueOnElement(
95+
manual.querySelector("input[name*=Latitude]"),
96+
item.y || ""
97+
);
7498
}
7599

76100
var event = new Event("addressselected", { bubbles: true });
@@ -81,6 +105,10 @@ document.addEventListener("DOMContentLoaded", function () {
81105
toggle?.addEventListener("click", function (e) {
82106
e.preventDefault();
83107

108+
if (!manual) {
109+
return;
110+
}
111+
84112
if (
85113
manual.style.display === "none" ||
86114
manual.style.display === ""
@@ -97,7 +125,9 @@ document.addEventListener("DOMContentLoaded", function () {
97125

98126
// Focus event to hide manual
99127
input?.addEventListener("focus", function () {
100-
manual.style.display = "none";
128+
if (manual) {
129+
manual.style.display = "none";
130+
}
101131
});
102132
};
103133

@@ -106,5 +136,6 @@ document.addEventListener("DOMContentLoaded", function () {
106136
setupAddressFinderField(elem);
107137
});
108138

139+
// @ts-ignore
109140
window.setupAddressFinderField = setupAddressFinderField;
110141
});

src/EditableAddressFinderField.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class EditableAddressFinderField extends EditableFormField
2323
'ShowManualFields' => 'Boolean',
2424
];
2525

26+
private $recordData = null;
27+
2628
/**
2729
* @return FieldList
2830
*/
@@ -58,4 +60,51 @@ public function getFormField()
5860

5961
return $field;
6062
}
63+
64+
65+
public function getValueFromData($data)
66+
{
67+
$this->recordData = isset($data[$this->Name]) ? $data[$this->Name] : null;
68+
69+
if ($this->recordData && is_array($this->recordData)) {
70+
if (isset($this->recordData['Address'])) {
71+
return $this->recordData['Address'];
72+
}
73+
74+
// no address found so return all the manul fields imploded
75+
$manual = [];
76+
77+
foreach ($this->recordData as $key => $value) {
78+
if ($key === 'Latitude' || $key === 'Longitude') {
79+
continue;
80+
}
81+
82+
if ($key !== 'Address') {
83+
$manual[] = $value;
84+
}
85+
}
86+
87+
$latLng = '';
88+
89+
if (isset($this->recordData['Latitude']) && isset($this->recordData['Longitude'])) {
90+
$latLng = ' (' . $this->recordData['Latitude'] . ', ' . $this->recordData['Longitude'] . ')';
91+
}
92+
93+
return implode(', ', $this->manual) . $latLng;
94+
}
95+
96+
return '';
97+
}
98+
99+
100+
public function getSubmittedFormField()
101+
{
102+
return SubmittedAddressField::create();
103+
}
104+
105+
106+
public function getRecordData()
107+
{
108+
return $this->recordData;
109+
}
61110
}

src/SubmittedAddressField.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace FullscreenInteractive\SilverStripe;
4+
5+
use SilverStripe\UserForms\Model\Submission\SubmittedFormField;
6+
7+
if (!class_exists(SubmittedFormField::class)) {
8+
return;
9+
}
10+
11+
class SubmittedAddressField extends SubmittedFormField
12+
{
13+
private static $table_name = 'SubmittedAddressField';
14+
15+
private static $extensions = [
16+
SubmittedAddressFieldExtension::class
17+
];
18+
19+
private static $db = [
20+
'PostalLine1' => 'Varchar(200)',
21+
'PostalLine2' => 'Varchar(200)',
22+
'PostalLine3' => 'Varchar(200)',
23+
'PostalLine4' => 'Varchar(200)',
24+
'PostalLine5' => 'Varchar(200)',
25+
'PostalLine6' => 'Varchar(200)',
26+
'Suburb' => 'Varchar(200)',
27+
'City' => 'Varchar(200)',
28+
'Postcode' => 'Varchar(200)',
29+
'Latitude' => 'Varchar(200)',
30+
'Longitude' => 'Varchar(200)'
31+
];
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace FullscreenInteractive\SilverStripe;
4+
5+
use SilverStripe\Core\Extension;
6+
7+
class SubmittedAddressFieldExtension extends Extension
8+
{
9+
10+
public function onPopulationFromField($field)
11+
{
12+
$recordData = $field->getRecordData();
13+
14+
$keys = [
15+
'PostalLine1',
16+
'PostalLine2',
17+
'PostalLine3',
18+
'PostalLine4',
19+
'PostalLine5',
20+
'PostalLine6',
21+
'Suburb',
22+
'City',
23+
'Postcode',
24+
'Latitude',
25+
'Longitude'
26+
];
27+
28+
foreach ($keys as $key) {
29+
if (isset($recordData[$key])) {
30+
$this->owner->$key = $recordData[$key];
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)