I’m trying to leverage the new API to construct custom fieldtypes. The userguide documentation doesn’t explain in great detail how to specifically use the save($data) and post_save($data) methods. I believe I should be able to retrieve the entry ID and field data from one of these two methods, but I have no idea how. I also have no idea how I can debug any issues associated with calling these methods.
If anyone has some insight into how to work with these methods, I would greatly appreciate a response.
Thanks, EE community.
Moved to Custom API Development by Moderator
Sue,
Thank you for responding. Can you provide any other information to help me move forward with custom field development (i.e. an example of making that method call and capturing the return data?) other than waiting on the documentation to evolve over time - which means there’s no timetable available.
Thank you
techyogi
Here are some tips:
-In the display_field() method, you are outputting your actual field. -In the save() method, your are “formatting” your POSTED data to me saved/passed_on. It always needs to return a STRING -In the post_save() method, the entry is already saved, and the supplied $data parameter is what you RETURNED from the save() method. -You only use the post_save() method if you need to custom stuff with the data (like inserting it your own sql tables)
Some people like examples more then text.
function display_field($data)
{
$field_name = $this->field_name
// Normally we use views here
$out = '<input name="' . $field_name . '" value="some_value">';
// Sometimes you forget to fill in field
// and you will send back to the form
// We need to fill the values in again.. *Sigh*
if ($this->EE->input->post($field_name) != FALSE)
{
// Normally we use views here
return '<input name="' . $field_name . '" value="' . $this->EE->input->post($field_name) .'">';
}
// This section is activated then you are EDITING an entry
if ($this->EE->input->get_post('entry_id') != FALSE)
{
// Grab entry data from your own sql_table (if you have one)
}
return $out;
}
// ********************************************************************************* //
function save($data)
{
// If your posted data is an array, serialize is first
// Since we always need to return a STRING
// This is how it is going to be saved in the exp_channels_data table
if (is_array($data) == TRUE)
{
return serialize($data);
}
else
{
return $data;
}
}
// ********************************************************************************* //
function post_save($data)
{
// $data in this case is what we returned from the save() method.
// It is always a string
// Since in my fields i always make use of arrays (read: <input name="field[food]" />
// I always need to unserialize my data to work with it.
$data = unserialize($data);
// The entry ID (not available in the save() method)
$entry_id = $this->settings['entry_id'];
$channel_id = $this->settings['channel_id'];
// DO SOME STUFF WITH YOUR DATA
// Entry is already saved, so no need to return anything here.
return;
}Hope it helps. I am not really the teacher guy but if you have any questions just ask.
concerning the post_save method, I’m having issues running any sort of insert / update query. I’m able to run my queries inside the save method - of course the entry / channel ids are not available which is a huge issue.
here’s what I’m trying to accomplish:
post_save($data) {
$data = unserialize($data);
$entry_id = $this->settings['entry_id'];
foreach ($data as $d) {
$mydata = array('entry_id' => $entry_id, 'some_data' => $d);
$this->EE->db->insert('exp_test_table', $mydata);
}
}Can anyone explain why this is not running, or is bombing on me?
I’ll second the post title…the documentation is somewhat helpful, but pretty vague overall. How do we know what global functions are available? I’m looking at the out-of-the-box fields for some help; I see many function calls and have no idea where they’re coming from or how to use them. Some are more obvious than others, but it sure would be nice to see a definitive guide of functions and their parameters. Here are a few:
lang decode_multi_field form_prep form_input form_multiselect form_dropdown form_textarea form_radio
I see some of those on the Form Generation Wiki page for CodeIgnitor, but it’s not clear to me where they’re coming from there either. http://codeigniter.com/wiki/Form_Generation/
I’ll second the post title…the documentation is somewhat helpful, but pretty vague overall. How do we know what global functions are available? I’m looking at the out-of-the-box fields for some help; I see many function calls and have no idea where they’re coming from or how to use them. Some are more obvious than others, but it sure would be nice to see a definitive guide of functions and their parameters. Here are a few: lang decode_multi_field form_prep form_input form_multiselect form_dropdown form_textarea form_radio I see some of those on the Form Generation Wiki page for CodeIgnitor, but it’s not clear to me where they’re coming from there either. http://codeigniter.com/wiki/Form_Generation/
Some of them come from: http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html http://ellislab.com/codeigniter/user-guide/libraries/validation.html http://ellislab.com/codeigniter/user-guide/libraries/language.html
Thanks, DevDemon!! Just FYI, the last two of those links are deprecated libraries. Here are the replacement links: http://ellislab.com/codeigniter/user-guide/helpers/language_helper.html http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.