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()
    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&region=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.

2 Answers 2


See Brandon's post on this here: What is getContent() / the ContentModel, and how do I use it to get and set custom field values?

Basically when you're setting new custom field values, you should use setContentFromPost

    'latitude' => $json->results[0]->geometry->location->lat,
  • Thank you. I was close enough with the setRawPostContent(). I should've kept digging. Commented Apr 16, 2015 at 17:36

Not sure if this is the recommended way to do this, but this works:

$entry->getContent()->myField = 'myValue';

So in your case probably this:

$entry->getContent()->latitude = $json->results[0]->geometry->location->lat;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.