2

I am building a field type for Craft that needs to do additional processing to the saved data once the element id is available. For this reason, the prepValueFromPost() method won't work, since the element id isn't available yet.

So how to update the content of the current element from the onAfterElementSave() method?

4

This will return the content for the specific element instance you're on:

$this->element->getContent()

And from there you can do your additional content processing.

When you're ready so save it, you can call:

craft()->content->saveContent($this->element);

There are several examples in craft/app/fieldtypes folder where Craft's native field types do additional processing like this.

3
  • So if I update a property on the ContentModel, how does that data get saved to the db? When I update my property, the new data isn't saved to the db and you can't call save() on models. Aug 13 '14 at 21:41
  • I figured it out. I just ran the following code to save the content. craft()->content->saveContent($this->element); If there is a better way, let me know. If not, may be helpful to update your answer with that code and I'll mark it as correct. Aug 13 '14 at 21:58
  • That's the way... updated!
    – Brad Bell
    Aug 13 '14 at 22:09

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.