I'm trying to build my first, very simple, one file plugin. I'm having difficulty with a basic task and I can't find any documentation to help me out. Keep in mind that there is no validation whatsoever, I'm just trying to get it to work first.
public function init()
{
parent::init();
craft()->on('entries.onBeforeSaveEntry', function(Event $event)
{
$entry = $event->params['entry'];
$section = craft()->sections->getSectionById($entry['sectionId']);
if ($section['handle'] == 'institutions')
{
$full_address = urlencode(rtrim($entry['adresse'].','.$entry['ville'].','.$entry['province'].','.$entry['codePostal']));
$json = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address={$full_address}&sensor=false®ion=ca&key=AIzaSyDhPYj9UwwUYNZYSz9Fo-j75gCJKWVdFeY"));
$entry->setRawPostContent('latitude' ,$json->results[0]->geometry->location->lat);
$entry->setRawPostContent('longitude', $json->results[0]->geometry->location->lng);
}
});
}
I have a listener on entries.onBeforeSaveEntry and it works well. I check if the section handle is what I need and then I look into the entry data to grab the address fields and query Google map. All of that is working like a charm.
I can't figure out how to put the data back into the entry for the lat and long filed that exists in my entry type.
$entry['latitude'] = $json->results[0]->geometry->location->lat;
That gives me an error saying latitude is not defined, which is false because I can output the value from it.
Also tried:
$entry->setRawPostContent('latitude' ,$json->results[0]->geometry->location->lat);
Any idea? I can't find any documentation or google search that can help me out.