Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T10:55:13.577Z
Answers.Text: Corrected code: forgot to use the `params` array.
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T19:56:12.213Z
Answers.Text: 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).
Answers.UserDisplayName: Chris
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T20:11:05.477Z
Answers.Text: Is `partnumber` a [number field-type](http://buildwithcraft.com/docs/number-fields)?
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T20:23:15.813Z
Answers.Text: 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.
Answers.UserDisplayName: Chris
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T20:31:07.533Z
Answers.Text: 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`?
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T20:53:37.413Z
Answers.Text: That's exactly what it looks like, yes. Just tried this:
  `if (is_numeric($event->params['entry']->partnumber)) {
       $event->params['entry']->partnumber = '123'; `
Answers.UserDisplayName: Chris
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T20:58:12.700Z
Answers.Text: I'm confused, check again if the field_handle is correct. If it is, I have no idea why it doesn't work, sorry.
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T21:17:19.903Z
Answers.Text: 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.)
Answers.UserDisplayName: Chris
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T21:33:03.367Z
Answers.Text: @Chris I just added that `->getContent()` to the last line of code in Victor’s example – make sure you add that as well.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T21:35:30.430Z
Answers.Text: @BrandonKelly Thanks, for helping, should've tested saving, not just getting. Are there any docs on those functions?
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T21:40:52.970Z
Answers.Text: @BrandonKelly That was the missing key. By gosh I think we've got it!
Answers.UserDisplayName: Chris
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T21:42:09.370Z
Answers.Text: @VictorIn Sorry not yet. We’re working on it though!
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T21:43:06.247Z
Answers.Text: Also - you may want to edit the answer to include the ->getContent() in the check if field is a number section.
Answers.UserDisplayName: Chris
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-17T21:50:33.970Z
Answers.Text: @Chris If I understand it correctly you need `->getContent()` only when setting a value, getting worked without it in my tests.
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-16T12:23:05.657Z
Answers.LastActivityDate: 2014-07-17T21:32:23.150Z
Answers.LastEditDate: 2014-07-17T21:32:23.150Z
Answers.UserDisplayName: Victor
Body: <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>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-16T10:27:46.990Z
Comments.Text: How easy would it be to check when the part number field is filled in via ajax & validate it there?
Comments.UserDisplayName: Seán O'Grady
Tags: <plugin-development><entries><entries-field>
Title: How could I have a plugin hook into when I edit or update a post?
ThreadText: # 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](http://buildwithcraft.com/docs/plugins/hooks-and-events) which get invoked at different points in the core code. In your case the
  [`entries.onBeforeSaveEntry`](http://buildwithcraft.com/docs/plugins/events-reference#entries-onBeforeSaveEntry) event would be perfect.
  
  **Example:**
  
  ```php
  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](http://buildwithcraft.com/docs/plugins/setting-things-up).
  
  > **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**: @username_3 Thanks, for helping, should've tested saving, not just getting. Are there any docs on those functions?  
  > **username_0**: @username_3 That was the missing key. By gosh I think we've got it!  
  > **username_3**: @VictorIn 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.  
  
  ---
  
  