<p>See Brandon's post on this here:
<a href="https://craftcms.stackexchange.com/questions/4486/what-is-getcontent-the-contentmodel-and-how-do-i-use-it-to-get-and-set-cust/4487#4487">What is getContent() / the ContentModel, and how do I use it to get and set custom field values?</a></p>

<p>Basically when you're setting new custom field values, you should use <a href="http://buildwithcraft.com/classreference/models/BaseElementModel#setContentFromPost-detail" rel="nofollow noreferrer"><code>setContentFromPost</code></a></p>

<pre><code>$entry-&gt;setContentFromPost(array(
    'latitude' =&gt; $json-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat,
);
</code></pre>

CC BY-SA 3.0
2015-04-16T17:36:43.660Z
Thank you.  I was close enough with the setRawPostContent().  I should've kept digging.
Eric Chantigny
CC BY-SA 3.0
2015-04-16T17:15:06.833Z
2015-04-16T17:15:06.833Z
2017-04-13T12:50:59.930Z
Peter Tell
<p>Not sure if this is the recommended way to do this, but this works:</p>

<pre><code>$entry-&gt;getContent()-&gt;myField = 'myValue';
</code></pre>

<p>So in your case probably this:</p>

<pre><code>$entry-&gt;getContent()-&gt;latitude = $json-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat;
</code></pre>

CC BY-SA 3.0
2015-04-16T16:41:13.147Z
2015-04-16T16:46:31.210Z
2015-04-16T16:46:31.210Z
carlcs
<p>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.</p>

<pre><code>public function init()
{
    parent::init();
    craft()-&gt;on('entries.onBeforeSaveEntry', function(Event $event)
    {
        $entry = $event-&gt;params['entry'];
        $section = craft()-&gt;sections-&gt;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}&amp;sensor=false&amp;region=ca&amp;key=AIzaSyDhPYj9UwwUYNZYSz9Fo-j75gCJKWVdFeY"));
            $entry-&gt;setRawPostContent('latitude' ,$json-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat);
            $entry-&gt;setRawPostContent('longitude', $json-&gt;results[0]-&gt;geometry-&gt;location-&gt;lng);
        }
    });
}
</code></pre>

<p>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.</p>

<p>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.  </p>

<pre><code>$entry['latitude'] = $json-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat;
</code></pre>

<p>That gives me an error saying latitude is not defined, which is false because I can output the value from it.</p>

<p>Also tried:</p>

<pre><code>$entry-&gt;setRawPostContent('latitude' ,$json-&gt;results[0]-&gt;geometry-&gt;location-&gt;lat);
</code></pre>

<p>Any idea?  I can't find any documentation or google search that can help me out.</p>

<plugin-development><plugins>
onBeforeSaveEntry listener to add/modify data of some fields
# onBeforeSaveEntry listener to add/modify data of some fields

(Asked by: username_0 on 2015-04-16)

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&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.

**Tags:** plugin-development, plugins

---

## 2 answers

### Answer by username_1 on 2015-04-16. Score: 2 | Accepted answer

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
$entry->setContentFromPost(array(
    'latitude' => $json->results[0]->geometry->location->lat,
);

> **Comments:**
> **username_0**: Thank you.  I was close enough with the setRawPostContent().  I should've kept digging.  

---

### Answer by username_2 on 2015-04-16. Score: 1

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;


---

