3

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.

  • How easy would it be to check when the part number field is filled in via ajax & validate it there? – Sean O'Grady Jul 16 '14 at 10:27
8

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.

| improve this answer | |
  • Corrected code: forgot to use the params array. – Victor In Jul 17 '14 at 10:55
  • 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 Jul 17 '14 at 19:56
  • Is partnumber a number field-type? – Victor In Jul 17 '14 at 20:11
  • 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: craftcms.stackexchange.com/questions/615/… - but I'm not sure how or if this applies to my situation. – Chris Jul 17 '14 at 20:23
  • 1
    @VictorIn Sorry not yet. We’re working on it though! – Brandon Kelly Jul 17 '14 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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