Skip to content

Commit 63295a1

Browse files
authored
Merge pull request #6357 from WoltLab/6.2-google-maps-form-field
FormBuilder form field for selecting a map location using Google Maps
2 parents 8bbc05d + 99afb6a commit 63295a1

7 files changed

Lines changed: 183 additions & 1 deletion

File tree

com.woltlab.wcf/templates/googleMapsElement.tpl renamed to com.woltlab.wcf/templates/shared_googleMapsElement.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@
1717
></woltlab-core-google-maps>
1818

1919
{if $googleMapsHidden}
20-
{include file='messageUserConsent' host="maps.google.com" url="https://www.google.com/maps/" target=$googleMapsElementID sandbox=true}
20+
{include file='shared_messageUserConsent' host="maps.google.com" url="https://www.google.com/maps/" target=$googleMapsElementID sandbox=true}
2121
{/if}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{capture assign='googleMapsElementID'}{$field->getPrefixedId()}_map{/capture}
2+
{include file='shared_googleMapsElement' accessUserLocation=true googleMapsLat=$field->getLatitude() googleMapsLng=$field->getLongitude()}
3+
4+
<input
5+
type="text"
6+
id="{$field->getPrefixedId()}"
7+
name="{$field->getPrefixedId()}"
8+
{if !$field->getFieldClasses()|empty} class="{implode from=$field->getFieldClasses() item='class' glue=' '}{$class}{/implode}"{/if}
9+
value="{$field->getValue()}"
10+
{if $field->isAutofocused()} autofocus{/if}
11+
{if $field->isRequired()} required{/if}
12+
{if $field->isImmutable()} disabled{/if}
13+
{if $field->getPlaceholder() !== null} placeholder="{$field->getPlaceholder()}"{/if}
14+
{if $field->getDocument()->isAjax()} data-dialog-submit-on-enter="true"{/if}
15+
{foreach from=$field->getFieldAttributes() key='attributeName' item='attributeValue'} {$attributeName}="{$attributeValue}"{/foreach}
16+
data-google-maps-geocoding="{$googleMapsElementID}"
17+
data-google-maps-geocoding-store="{$field->getPrefixedId()}_"
18+
data-google-maps-marker
19+
>
File renamed without changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Data handler for a Google Maps form builder field in an Ajax form.
3+
*
4+
* @author Marcel Werk
5+
* @copyright 2001-2025 WoltLab GmbH
6+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
7+
* @since 6.2
8+
*/
9+
import { FormBuilderData } from "../Data";
10+
import Value from "./Value";
11+
import type WoltlabCoreGoogleMapsElement from "WoltLabSuite/Core/Component/GoogleMaps/woltlab-core-google-maps";
12+
13+
class GoogleMaps extends Value {
14+
protected _getData(): FormBuilderData {
15+
const map = document.getElementById(this._fieldId + "_map") as WoltlabCoreGoogleMapsElement;
16+
17+
return {
18+
[this._fieldId]: (this._field as HTMLInputElement).value,
19+
[this._fieldId + "_coordinates"]: `${map.lat},${map.lng}`,
20+
};
21+
}
22+
}
23+
24+
export = GoogleMaps;

wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Field/GoogleMaps.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace wcf\system\form\builder\field;
4+
5+
use wcf\system\form\builder\data\processor\CustomFormDataProcessor;
6+
use wcf\system\form\builder\IFormDocument;
7+
8+
/**
9+
* Implementation of a form field for selecting map coordinates.
10+
*
11+
* @author Marcel Werk
12+
* @copyright 2001-2025 WoltLab GmbH
13+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14+
* @since 6.2
15+
*/
16+
final class GoogleMapsFormField extends AbstractFormField implements
17+
IAttributeFormField,
18+
IAutoFocusFormField,
19+
ICssClassFormField,
20+
IImmutableFormField,
21+
IPlaceholderFormField
22+
{
23+
use TInputAttributeFormField {
24+
getReservedFieldAttributes as private getDefaultReservedFieldAttributes;
25+
}
26+
use TAutoFocusFormField;
27+
use TCssClassFormField;
28+
use TImmutableFormField;
29+
use TPlaceholderFormField;
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
protected $javaScriptDataHandlerModule = 'WoltLabSuite/Core/Form/Builder/Field/GoogleMaps';
35+
36+
/**
37+
* @inheritDoc
38+
*/
39+
protected $templateName = 'shared_googleMapsFormField';
40+
41+
private float $latitude = 0;
42+
private float $longitude = 0;
43+
44+
public function __construct()
45+
{
46+
$this->addFieldClass('long');
47+
}
48+
49+
/**
50+
* @return string[]
51+
*/
52+
protected static function getReservedFieldAttributes(): array
53+
{
54+
return \array_merge(
55+
static::getDefaultReservedFieldAttributes(),
56+
[
57+
'data-google-maps-geocoding-store',
58+
'data-google-maps-geocoding',
59+
'data-google-maps-marker',
60+
]
61+
);
62+
}
63+
64+
#[\Override]
65+
public function readValue()
66+
{
67+
if ($this->getDocument()->hasRequestData($this->getPrefixedId())) {
68+
$this->value = $this->getDocument()->getRequestData($this->getPrefixedId());
69+
}
70+
71+
if ($this->getDocument()->hasRequestData($this->getPrefixedId() . '_coordinates')) {
72+
$coordinates = explode(',', $this->getDocument()->getRequestData(
73+
$this->getPrefixedId() . '_coordinates'
74+
));
75+
if (\count($coordinates) === 2) {
76+
$this->latitude = \floatval($coordinates[0]);
77+
$this->longitude = \floatval($coordinates[1]);
78+
}
79+
}
80+
81+
return $this;
82+
}
83+
84+
#[\Override]
85+
public function populate()
86+
{
87+
parent::populate();
88+
89+
$this->getDocument()->getDataHandler()->addProcessor(new CustomFormDataProcessor(
90+
'coordinates',
91+
function (IFormDocument $document, array $parameters) {
92+
if ($this->getValue()) {
93+
$parameters[$this->getPrefixedId() . '_coordinates'] = [
94+
'latitude' => $this->getLatitude(),
95+
'longitude' => $this->getLongitude(),
96+
];
97+
}
98+
99+
return $parameters;
100+
}
101+
));
102+
103+
return $this;
104+
}
105+
106+
public function getLatitude(): float
107+
{
108+
return $this->latitude;
109+
}
110+
111+
public function getLongitude(): float
112+
{
113+
return $this->longitude;
114+
}
115+
116+
public function coordinates(float $latitude, float $longitude): static
117+
{
118+
$this->latitude = $latitude;
119+
$this->longitude = $longitude;
120+
121+
return $this;
122+
}
123+
}

wcfsetup/install/files/lib/system/template/TemplateEngine.class.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,13 @@ class TemplateEngine extends SingletonFactory
109109
'formError' => 'shared_formError',
110110
'formNotice' => 'shared_formNotice',
111111
'formSuccess' => 'shared_formSuccess',
112+
'googleMapsElement' => 'shared_googleMapsElement',
112113
'languageChooser' => 'shared_languageChooser',
113114
'lineBreakSeparatedTextOptionType' => 'shared_lineBreakSeparatedTextOptionType',
114115
'mediaManager' => 'shared_mediaManager',
115116
'messageFormAttachments' => 'shared_messageFormAttachments',
116117
'messageTableOfContents' => 'shared_messageTableOfContents',
118+
'messageUserConsent' => 'shared_messageUserConsent',
117119
'multipleLanguageInputJavascript' => 'shared_multipleLanguageInputJavascript',
118120
'passwordStrengthLanguage' => 'shared_passwordStrengthLanguage',
119121
'quoteMetaCode' => 'shared_quoteMetaCode',

0 commit comments

Comments
 (0)