Skip to content

ujamii/ujamii-geocoder

Repository files navigation

ujamii-geocoder

Packagist

Connecting http://geocoder-php.org/Geocoder with TYPO3 DataHandler API. With this extension you can easily add geo data to records while they are changed by editors in the TYPO3 backend. You just have to configure how your entity "looks like" in the eyes of the geocoder via TCA anf that's it.

Installation

Currently only works in composer mode of TYPO3, so

composer require ujamii/ujamii-geocoder

Usage

Just add a new config array to the ctrl section of your TCA.

..['ctrl']['geocoder'] = [
	'triggerFields' => ['street', 'zip', 'city'],
	'getAddressString' => 'Your\Namespace\Domain\Model\YourEntity->getAddressString'
];

And provide a method (e.g. in an entity or helper class) to generate a compound address string based on the database data of one entity (example below).

Options

Those options are possible:

triggerFields (mandatory)

Changes in those fields will trigger the process of geocoding.

getAddressString (mandatory)

A method called via TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction which is supposed to return a complete address string. The first parameter provided to this method is the merged data array (unchanged entity data from db + changed values from the backend form).

Example:

public function getAddressString($dataArray) {
	return $dataArray['location'];
}

locale (default: de)

The locale which is used in the geocoder.

latField (default: lat)

Name of the target field for the latitude value.

lngField (default: lng)

Name of the target field for the longitude value.

httpClientClass (default: \Http\Adapter\Guzzle6\Client::class)

Class name of the http client, see possible packages

providerClass (default: \Geocoder\Provider\GoogleMaps\GoogleMaps::class)

Class name of the provider, see possible packages

providerParams

Optional parameters for the provider. (e.g. an API key for Google Maps)

geocoderClass (default: \Geocoder\StatefulGeocoder::class)

Class name of the geocoder.

Example

Let's assume the record is a news record in the database with 3 fields: location, lat and lng. The field location contains something like Alexanderplatz, Berlin, Deutschland and lat and lng are the fields you want to be filled automatically as soon as an editor changes the location.

Add this to your typo3conf/ext/your_extension/Configuration/TCA/Overrides/tx_news_domain_model_news.php or typo3conf/ext/your_extension/Configuration/TCA/tx_ext_domain_model_entity.php file.

$GLOBALS['TCA']['tx_news_domain_model_news']['ctrl']['geocoder'] = [
	'triggerFields' => ['street', 'zip', 'city'],
	'getAddressString' => 'Your\Namespace\Domain\Model\YourEntity->getAddressString',
	'providerParams' => [
        0 => null,
        1 => null,
        2 => '<GMAPS_API_KEY>'
    ]
];

In YourEntity, add a method like this:

public function getAddressString($dataArray) {
	return sprintf('%s, %s %s', $dataArray['street'], $dataArray['zip'], $dataArray['city']);
}

Usage as command or in scheduler

The extension also provides command to populate rows with 0 values for the lat/lng fields. It reads the configuration from TCA and iterates through each configured table, searching with lat = 0 OR lng = 0. For each matching row, the geocoding process is executed and the values are then updated in the database. The command produces some log output to track what has been done.

As it is a default Symfony console command, this can also be called by a scheduler task.

vendor/bin/typo3 geocoder:fillmissingdata

TODOs

  • publish in TER
  • right now providerParams[0] is always filled with the httpClient, which may not work for all providers