<p>Craft has <a href="http://buildwithcraft.com/docs/plugins/hooks-and-events" rel="noreferrer">Hooks and Events</a> which get invoked at different points in the core code. In your case the
<a href="http://buildwithcraft.com/docs/plugins/events-reference#entries-onBeforeSaveEntry" rel="noreferrer"><code>entries.onBeforeSaveEntry</code></a> event would be perfect.</p>

<p><strong>Example:</strong></p>

<pre class="lang-php prettyprint-override"><code>public function init()
{
    parent::init();

    //Event: onBeforeSaveEntry
    craft()-&gt;on('entries.onBeforeSaveEntry', function(Event $event)
    {
        //Is the entry in the section 'parts'?
        if ($event-&gt;params['entry']-&gt;section == 'parts') {

            //Check if field is a number
            if (is_numeric($event-&gt;params['entry']-&gt;number_field_handle)) {
                //Do your JSON stuff

                //Replace values
                $event-&gt;params['entry']-&gt;getContent()-&gt;number_field_handle = 'something_else';
            }

        }
    });
}
</code></pre>

<p>The <code>init()</code> function goes into your <a href="http://buildwithcraft.com/docs/plugins/setting-things-up" rel="noreferrer">Primary Plugin Class</a>.</p>

CC BY-SA 3.0
2014-07-17T10:55:13.577Z
Corrected code: forgot to use the `params` array.
Victor
CC BY-SA 3.0
2014-07-17T19:56:12.213Z
Although this pointed me in the right direction I am having issues using custom field handles that I've added to the entry. For example: If I try to use the handle 'partnumber' which contains the number, it throws the error: Property "Craft\EntryModel.partnumber" is not defined. (Even though partnumber is the handle of that particular field). When I use, say, title however - it recognizes that (but does not allow me to overwrite it due to that field being read only).
Chris
CC BY-SA 3.0
2014-07-17T20:11:05.477Z
Is `partnumber` a [number field-type](http://buildwithcraft.com/docs/number-fields)?
Victor
CC BY-SA 3.0
2014-07-17T20:23:15.813Z
It is, yes. I've even tried tried removing  the (if(is_int line and just having it update the value regardless and am still getting the not defined error. I found this here: http://craftcms.stackexchange.com/questions/615/update-entry-programatically  - but I'm not sure how or if this applies to my situation.
Chris
CC BY-SA 3.0
2014-07-17T20:31:07.533Z
Just checked, it should be [`is_numeric()`](http://php.net/manual/de/function.is-numeric.php) and not [`is_int()`](http://php.net/manual/de/function.is-int.php). But I don't understand why you get the error. Does your code look like this: `$event->params['entry']->partnumber`?
Victor
CC BY-SA 3.0
2014-07-17T20:53:37.413Z
That's exactly what it looks like, yes. Just tried this:
`if (is_numeric($event->params['entry']->partnumber)) {
     $event->params['entry']->partnumber = '123'; `
Chris
CC BY-SA 3.0
2014-07-17T20:58:12.700Z
I'm confused, check again if the field_handle is correct. If it is, I have no idea why it doesn't work, sorry.
Victor
CC BY-SA 3.0
2014-07-17T21:17:19.903Z
It's correct. I've tried many different field handles - none of which work (except for things listed in the EntryModel properties (http://buildwithcraft.com/docs/templating/entrymodel) like title, slug, etc.)
Chris
CC BY-SA 3.0
2014-07-17T21:33:03.367Z
@Chris I just added that `->getContent()` to the last line of code in Victor’s example – make sure you add that as well.
Brandon Kelly
CC BY-SA 3.0
2014-07-17T21:35:30.430Z
@BrandonKelly Thanks, for helping, should've tested saving, not just getting. Are there any docs on those functions?
Victor
CC BY-SA 3.0
2014-07-17T21:40:52.970Z
@BrandonKelly That was the missing key. By gosh I think we've got it!
Chris
CC BY-SA 3.0
2014-07-17T21:42:09.370Z
@VictorIn Sorry not yet. We’re working on it though!
Brandon Kelly
CC BY-SA 3.0
2014-07-17T21:43:06.247Z
Also - you may want to edit the answer to include the ->getContent() in the check if field is a number section.
Chris
CC BY-SA 3.0
2014-07-17T21:50:33.970Z
@Chris If I understand it correctly you need `->getContent()` only when setting a value, getting worked without it in my tests.
Victor
CC BY-SA 3.0
2014-07-16T12:23:05.657Z
2014-07-17T21:32:23.150Z
2014-07-17T21:32:23.150Z
Victor
<p>What I am trying to do is have my plugin know when I edit a post that is part of a particular section - let's call it "parts" - which then checks to see if a field is filled out in the form and then hits up an API and then pulls information from it and inserts it into empty fields in the entry.</p>

<p>So to clarify, here is a flow of what I'm looking to do:</p>

<ol>
<li>I create an entry in the section "parts". </li>
<li>Upon saving, it checks to see if the part number field is filled in.</li>
<li>If the field is filled in it will access an external json api and I'll pull a certain value (or in some cases multiple values) from the array.</li>
<li>I then want to have it fill in/update/replace the values in fields that I specify that are part of this entry. </li>
</ol>

<p>So essentially I am trying to figure out how to: Have my plugin trigger once an entry in a specific section is edited or added and then how I can (off of this trigger) have the plugin insert a value (or multiple values) into specific fields in the entry.</p>

CC BY-SA 3.0
2014-07-16T10:27:46.990Z
How easy would it be to check when the part number field is filled in via ajax & validate it there?
Seán O'Grady
<plugin-development><entries><entries-field>
How could I have a plugin hook into when I edit or update a post?
# How could I have a plugin hook into when I edit or update a post?

(Asked by: username_0 on 2014-07-16)

What I am trying to do is have my plugin know when I edit a post that is part of a particular section - let's call it "parts" - which then checks to see if a field is filled out in the form and then hits up an API and then pulls information from it and inserts it into empty fields in the entry.

So to clarify, here is a flow of what I'm looking to do:

1. I create an entry in the section "parts".
2. Upon saving, it checks to see if the part number field is filled in.
3. If the field is filled in it will access an external json api and I'll pull a certain value (or in some cases multiple values) from the array.
4. I then want to have it fill in/update/replace the values in fields that I specify that are part of this entry.

So essentially I am trying to figure out how to: Have my plugin trigger once an entry in a specific section is edited or added and then how I can (off of this trigger) have the plugin insert a value (or multiple values) into specific fields in the entry.

**Tags:** plugin-development, entries, entries-field

> **Comments:**
> **username_1**: How easy would it be to check when the part number field is filled in via ajax & validate it there?  

---

## 1 answers

### Answer by username_2 on 2014-07-16. Score: 8 | Accepted answer

Craft has Hooks and Events which get invoked at different points in the core code. In your case the
entries.onBeforeSaveEntry event would be perfect.
Example:
public function init()
{
    parent::init();

    //Event: onBeforeSaveEntry
    craft()->on('entries.onBeforeSaveEntry', function(Event $event)
    {
        //Is the entry in the section 'parts'?
        if ($event->params['entry']->section == 'parts') {

            //Check if field is a number
            if (is_numeric($event->params['entry']->number_field_handle)) {
                //Do your JSON stuff

                //Replace values
                $event->params['entry']->getContent()->number_field_handle = 'something_else';
            }

        }
    });
}

The init() function goes into your Primary Plugin Class.

> **Comments:**
> **username_2**: Corrected code: forgot to use the `params` array.  
> **username_0**: Although this pointed me in the right direction I am having issues using custom field handles that I've added to the entry. For example: If I try to use the handle 'partnumber' which contains the number, it throws the error: Property "Craft\EntryModel.partnumber" is not defined. (Even though partnumber is the handle of that particular field). When I use, say, title however - it recognizes that (but does not allow me to overwrite it due to that field being read only).  
> **username_2**: Is `partnumber` a [number field-type](http://buildwithcraft.com/docs/number-fields)?  
> **username_0**: It is, yes. I've even tried tried removing  the (if(is_int line and just having it update the value regardless and am still getting the not defined error. I found this here: http://craftcms.stackexchange.com/questions/615/update-entry-programatically  - but I'm not sure how or if this applies to my situation.  
> **username_2**: Just checked, it should be [`is_numeric()`](http://php.net/manual/de/function.is-numeric.php) and not [`is_int()`](http://php.net/manual/de/function.is-int.php). But I don't understand why you get the error. Does your code look like this: `$event->params['entry']->partnumber`?  
> **username_0**: That's exactly what it looks like, yes. Just tried this: `if (is_numeric($event->params['entry']->partnumber)) {      $event->params['entry']->partnumber = '123'; `  
> **username_2**: I'm confused, check again if the field_handle is correct. If it is, I have no idea why it doesn't work, sorry.  
> **username_0**: It's correct. I've tried many different field handles - none of which work (except for things listed in the EntryModel properties (http://buildwithcraft.com/docs/templating/entrymodel) like title, slug, etc.)  
> **username_3**: @username_0 I just added that `->getContent()` to the last line of code in username_2’s example – make sure you add that as well.  
> **username_2**: @BrandonKelly Thanks, for helping, should've tested saving, not just getting. Are there any docs on those functions?  
> **username_0**: @BrandonKelly That was the missing key. By gosh I think we've got it!  
> **username_3**: @username_2In Sorry not yet. We’re working on it though!  
> **username_0**: Also - you may want to edit the answer to include the ->getContent() in the check if field is a number section.  
> **username_2**: @username_0 If I understand it correctly you need `->getContent()` only when setting a value, getting worked without it in my tests.  

---

