Integrate the form element "Maps2PoiPosition" in code
New in version 13.3.0
maps2 provides the form element Maps2Poi for forms based on
the TYPO3 form framework (EXT:form).
The form element Maps2Poi shows a map with a draggable marker,
so frontend users can correct a position, which your extension has found
by geocoding an address. The chapter Form element "Maps2PoiPosition" for the form framework
describes the requirements, the properties and the output of the element.
This chapter is for developers of extensions, which use the element. It describes the value of the element, its validation and a complete example: a form, which geocodes the entered address and stores the corrected position.
Tasks of maps2 and of your extension
maps2 renders the map, writes the position of the marker into the form and validates the submitted position.
Your extension
- ships the form definition with the element,
- sets the initial position, for example after geocoding an address,
- stores the submitted position, for example in a finisher.
Value of the form element "Maps2PoiPosition"
The value of the element is an array with the keys latitude and
longitude. Both values are numeric strings:
[
'latitude' => '48.663012',
'longitude' => '9.218945',
]
The template of the element renders two hidden fields, which the JavaScript of maps2 updates with the position of the marker, rounded to 6 decimals:
tx_form_formframework[<form identifier>][<element identifier>][latitude]
tx_form_formframework[<form identifier>][<element identifier>][longitude]
After the submit, maps2 normalizes the value in a listener of the PSR-14
event TYPO3CMSForm:
- Both values are trimmed. Other keys than
latitudeandlongitudeare removed. - If both values are empty (the marker was not placed), the value is
null. So the validatorNotapplies.Empty
The class JWeiland converts
between a position and the value of the element:
use JWeiland\Maps2\Form\PoiPosition\PoiPosition;
// Returns null, if the value is not an array with numeric coordinates
$position = PoiPosition::fromFormValue($formValues['position'] ?? null);
$latitude = $position?->latitude;
// ['latitude' => '48.663012', 'longitude' => '9.218945']
$value = (new PoiPosition(48.6630123, 9.2189451))->toFormValue();
Initial position of the map
The map shows the value of the element in the following order:
- The value in the form state, for example the submitted value or a value, which an event listener has written into the form state.
- The default value of the element:
defaultin the form definition or a value set withValue set.Default Value ()
Both use the array format of the value. Only a valid position (numeric, latitude -90 to 90, longitude -180 to 180) is used.
Without a valid position, the marker is shown at the default position of
the extension settings (default,
default), but the hidden fields stay empty. So the element has no
value, until the user places the marker.
Validation of the form element "Maps2PoiPosition"
maps2 adds the validator Maps2Poi to each element automatically.
It accepts empty values. Add the validator Not to require a position.
A value, which is not empty, has to fulfill these rules:
|
Error code |
Rule |
Message |
|---|---|---|
|
|
Latitude and longitude are numeric. |
Please place the marker on the map. |
|
|
The latitude is between -90 and 90. |
The latitude must be between -90 and 90. |
|
|
The longitude is between -180 and 180. |
The longitude must be between -180 and 180. |
The validator translates the messages with the keys
validator., validator.
and validator. of the language file of
maps2. The error codes are available as constants of
JWeiland.
Override a message for your forms with the error code in a translation
file of EXT:form, for example validation. (all forms) or
<form identifier>.
(a single element). Register the translation file with
rendering of the form.
Templates of the form element "Maps2PoiPosition"
All paths of templates, JavaScript, style sheets and URLs are defined in
Fluid templates and in the form setup, not in PHP. The partials are in
EXT: (partial root path key
15) and use the file extension .fluid.:
Maps2PoiPosition - The element: hidden fields with the position and the map or the consent information.
Maps2PoiPosition/ Load Assets - JavaScript and style sheets of the map provider. See JavaScript and style sheets of the map.
Maps2PoiPosition/ Overlay - Consent information with the button to show the map.
Maps2PoiPosition/ Summary Value - The position as text on the summary page.
SummaryPage - The summary page of EXT:form with an additional branch for the element.
The event listener
JWeiland
(event TYPO3CMSForm)
prepares the
data of the map in the rendering option maps2Poi of the element:
latitude, longitude (values of the hidden fields), map,
map, google,
is, current, environment and
pois (JSON for the JavaScript of maps2).
Integrate the form element "Maps2PoiPosition" step by step
The following example uses a form with two pages: the user enters the address on the first page. When the page is submitted, an event listener geocodes the address. The second page shows the map with the found position, where the user can correct it. After the submit, a finisher stores the position as POI collection and another finisher stores the record with a relation to the POI collection.
Replace My, My and my_ with the names of
your extension.
Step 1: Create the form definition
type: Form
identifier: company
label: 'Company'
prototypeName: standard
renderables:
- type: Page
identifier: page-address
label: 'Address'
renderingOptions:
nextButtonLabel: 'Next step: position on the map'
renderables:
- type: Text
identifier: company
label: 'Company'
validators:
- identifier: NotEmpty
- type: Text
identifier: street
label: 'Street and house number'
validators:
- identifier: NotEmpty
- type: Text
identifier: zip
label: 'ZIP code'
validators:
- identifier: NotEmpty
- type: Text
identifier: city
label: 'City'
validators:
- identifier: NotEmpty
- type: Page
identifier: page-map
label: 'Position on the map'
renderables:
- type: Maps2PoiPosition
identifier: position
label: 'Position on the map'
properties:
markerTitle: 'Your company'
elementDescription: 'Move the marker, if the position is not correct.'
validators:
- identifier: NotEmpty
- type: SummaryPage
identifier: page-summary
label: 'Summary'
finishers:
- identifier: SavePosition
options:
storagePid: 12
title: '{company}'
- identifier: SaveToDatabase
options:
table: tx_myextension_domain_model_company
mode: insert
elements:
company:
mapOnDatabaseColumn: title
street:
mapOnDatabaseColumn: street
zip:
mapOnDatabaseColumn: zip
city:
mapOnDatabaseColumn: city
databaseColumnMappings:
pid:
value: 12
tx_maps2_uid:
value: '{SavePosition.poiCollectionUid}'
The column tx_ is the relation to the POI collection, which you
add to your table as TCA column with the render maps2Relation. See
Maps2 Registry.
Step 2: Register the form and the finisher
Allow EXT:form to load forms of your extension and register the finisher
Save in a form set of your extension. EXT:form discovers form sets
in Configuration/ of all installed extensions:
name: my-vendor/my-extension
label: 'My extension'
persistenceManager:
allowedExtensionPaths:
1763390000: 'EXT:my_extension/Resources/Private/Forms/'
prototypes:
standard:
finishersDefinition:
SavePosition:
implementationClassName: 'MyVendor\MyExtension\Form\Finisher\SavePositionFinisher'
The form set of maps2 (jweiland/) registers the element. The map
settings come from the site sets of maps2. Add one of them to the
dependencies of the site set of your extension:
name: my-vendor/my-extension
label: 'My extension'
dependencies:
- typo3/form
- jweiland/maps2-openstreetmap
Step 3: Geocode the address in an event listener
EXT:form dispatches the PSR-14 event
TYPO3CMSForm for the
submitted page first and then for each element of this page. The following
listener reacts to the page with the address, geocodes the address with the
JWeiland of maps2 and writes the
found position into the form state. Then the map on the next page shows
this position.
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\EventListener;
use JWeiland\Maps2\Domain\Model\Position;
use JWeiland\Maps2\Form\PoiPosition\PoiPosition;
use JWeiland\Maps2\Service\GeoCodeService;
use TYPO3\CMS\Core\Attribute\AsEventListener;
use TYPO3\CMS\Form\Domain\Model\FormElements\Page;
use TYPO3\CMS\Form\Event\BeforeRenderableIsValidatedEvent;
/**
* Geocodes the address, when the page with the address is submitted,
* and sets the found position as value of the element "position".
*/
#[AsEventListener(identifier: 'my-extension/geocode-address')]
final readonly class GeocodeAddressEventListener
{
private const ADDRESS_PAGE = 'page-address';
private const ADDRESS_ELEMENTS = ['street', 'zip', 'city'];
private const POSITION_ELEMENT = 'position';
/**
* Key in the form state with the last geocoded address
*/
private const GEOCODED_ADDRESS = '__myExtensionGeocodedAddress';
public function __construct(
private GeoCodeService $geoCodeService,
) {}
public function __invoke(BeforeRenderableIsValidatedEvent $event): void
{
$formState = $event->formRuntime->getFormState();
if (!$event->renderable instanceof Page
|| $event->renderable->getIdentifier() !== self::ADDRESS_PAGE
|| $formState === null
) {
return;
}
// Geocode an unchanged address not again. So a corrected
// position is kept, if the user goes back to the address.
$address = $this->buildAddress($event->request->getArguments());
if ($address === ''
|| $address === $formState->getFormValue(self::GEOCODED_ADDRESS)
) {
return;
}
$position = $this->geoCodeService
->getFirstFoundPositionByAddress($address);
if ($position instanceof Position) {
$poiPosition = new PoiPosition(
$position->getLatitude(),
$position->getLongitude(),
);
$formState->setFormValue(
self::POSITION_ELEMENT,
$poiPosition->toFormValue(),
);
$formState->setFormValue(self::GEOCODED_ADDRESS, $address);
}
}
/**
* @param array<string, mixed> $requestArguments
*/
private function buildAddress(array $requestArguments): string
{
$parts = [];
foreach (self::ADDRESS_ELEMENTS as $identifier) {
$value = $requestArguments[$identifier] ?? '';
$parts[] = is_string($value) ? trim($value) : '';
}
return trim(sprintf('%s, %s %s', ...$parts), ' ,');
}
}
The attribute # registers the listener. No entry in
ext_ is needed.
If the address can not be geocoded, the map shows the previous position or the default position of the extension settings and the user has to place the marker. Your listener can add an error to an element of the address page instead, so the user stays on the address page:
use TYPO3\CMS\Extbase\Error\Error;
$event->formRuntime->getFormDefinition()
->getProcessingRule('street')
->getProcessingMessages()
->addError(new Error('The address was not found.', 1763390002));
The geocoding uses the map provider and the API keys of the extension settings.
Step 4: Show the stored position of an existing record
If the form edits an existing record, set the stored position as default
value of the element. Use a listener of an event, which has access to the
request, for example
TYPO3CMSForm:
use JWeiland\Maps2\Form\PoiPosition\PoiPosition;
// $latitude and $longitude of the POI collection of the edited record
$position = new PoiPosition($latitude, $longitude);
$event->formRuntime->getFormDefinition()
->getElementByIdentifier('position')
?->setDefaultValue($position->toFormValue());
A value in the form state wins over the default value. So the geocoding listener of step 3 replaces the stored position, as soon as the address page is submitted. Compare the submitted address with the stored address of the record and skip the geocoding, if the address was not changed.
Step 5: Store the position in a finisher
The following finisher creates a POI collection of type Point with the
submitted position. It provides the UID of the new record as finisher
variable Save for the following finishers, see
step 1.
<?php
declare(strict_types=1);
namespace MyVendor\MyExtension\Form\Finisher;
use JWeiland\Maps2\Form\PoiPosition\PoiPosition;
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;
/**
* Stores the position of the element "Maps2PoiPosition" as new POI
* collection of type "Point".
*/
#[Autoconfigure(public: true, shared: false)]
class SavePositionFinisher extends AbstractFinisher
{
private const TABLE = 'tx_maps2_domain_model_poicollection';
protected $defaultOptions = [
'elementIdentifier' => 'position',
'storagePid' => 0,
'title' => '',
];
public function __construct(
private readonly ConnectionPool $connectionPool,
) {}
protected function executeInternal(): ?string
{
$formValues = $this->finisherContext->getFormValues();
$position = PoiPosition::fromFormValue(
$formValues[(string)$this->parseOption('elementIdentifier')] ?? null,
);
if ($position === null) {
return null;
}
$connection = $this->connectionPool->getConnectionForTable(self::TABLE);
$connection->insert(self::TABLE, [
'pid' => (int)$this->parseOption('storagePid'),
'tstamp' => time(),
'crdate' => time(),
'collection_type' => 'Point',
'title' => (string)$this->parseOption('title'),
'latitude' => $position->latitude,
'longitude' => $position->longitude,
]);
$this->finisherContext->getFinisherVariableProvider()->add(
$this->shortFinisherIdentifier,
'poiCollectionUid',
(int)$connection->lastInsertId(),
);
return null;
}
}
To edit an existing record, update the POI collection, which is related to the record, instead of creating a new one.
Hint
The validator Maps2Poi has checked the position already.
Poi returns null, if the element is
optional and the user has not placed the marker.
Step 6: Show the form
Add the content element Form to a page and select the form Company, or render the form in a Fluid template of your extension:
<html xmlns:formvh="http://typo3.org/ns/TYPO3/CMS/Form/ViewHelpers"
data-namespace-typo3-fluid="true">
<formvh:render
persistenceIdentifier="EXT:my_extension/Resources/Private/Forms/Company.form.yaml"/>
</html>
Include the site set of your extension in the site, so the settings of the map are available. The form sets of your extension and of maps2 are loaded automatically.