Created
September 20, 2017 08:31
-
-
Save renebakx/a3b0959d3e6e2df375c45a339c124915 to your computer and use it in GitHub Desktop.
quick geocoder in D8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function geocoder_entity_presave(Drupal\Core\Entity\EntityInterface $entity) { | |
if ($entity->getEntityTypeId() == 'node' && $entity->bundle() == 'distributor'){ | |
$street = $entity->get('field_street')->getString(); | |
$city = $entity->get('field_city')->getString(); | |
$country = $entity->get('field_country')->getString(); | |
$adress = sprintf('%s, %s, %s',$street,$city,$country); | |
$geocode = new Geocode(); | |
$result = $geocode->get($adress); | |
$lat = $result->getLatitude(); | |
$long = $result->getLongitude(); | |
$entity->set('field_latitude',$lat); | |
$entity->set('field_longitude',$long); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And yes it could be a lot cleaner without using transporter variables for lat and long etc.