Skip to content

Instantly share code, notes, and snippets.

@renebakx
Created September 20, 2017 08:31
Show Gist options
  • Save renebakx/a3b0959d3e6e2df375c45a339c124915 to your computer and use it in GitHub Desktop.
Save renebakx/a3b0959d3e6e2df375c45a339c124915 to your computer and use it in GitHub Desktop.
quick geocoder in D8
<?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);
}
}
@renebakx
Copy link
Author

And yes it could be a lot cleaner without using transporter variables for lat and long etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment