I’ve been trying to develop a custom field, and it’s been fine for a while, but I’m now stumbling into some issues. I’m trying to use the display_settings() and save_settings() feature to allow users to choose from a selection of member groups with checkboxes.
The checkboxes are showing fine, but when it comes to submitting the Create Custom Field type, I get an SQL error as it’s trying to insert my dynamically created checkboxes straight from $_POST. I can see that save_settings() is being called, but as it’s been called via apply() I can’t modify $_POST or anything, and I can’t see what else there is to do.
I’m sure I’m missing a trick here, and if you could fill me in with how to do it I’d be very grateful 😊
Okay, update on the situation. It wasn’t working because save_settings() was never being called on an insert, only an edit. I added the following three lines on line 4577 of cp/admin_content.php:
$this->api_channel_fields->fetch_all_fieldtypes();
$this->api_channel_fields->setup_handler($_POST['field_type']);
$this->api_channel_fields->apply('save_settings', $_POST);And now I can unset the $_POST variables manually. The next step to find is how I get the settings to persist…
The next step to find is how I get the settings to persist…
Check out the readme for NSM TinyMCE. I figured out how to enable per field settings. You can use my code as an example of how to save / read them as well. It’s all really well commented.
Sure thing - although you pretty much have it all covered. In fact, you added a step that isn’t strictly necessary. The native array doesn’t need to be altered, since that key should never come in through POST.
1. Uncomment line 4470 (line number depends on your build - reads “base64_encode(serialize($ft_settings))” 2. In save_settings return an associative array. - Returning one of the native keys overrides - Your own keys are added to the serialized array - Everything you return is available on publish via $this->settings, but not on the frontend.
That’s pretty much it. Make sure you do your own data prepping, cleaning, etc. And remember that the EE superobject points to the current controller instance, so you can access _get_ft_post_data, which is helpful if you’re prefixing your fields.
Oh and don’t build your own workarounds for global fieldtype settings (ala field frame) just yet … cough cough.
- Returning one of the native keys overrides
Should’ve read “…overrides the native column”. So for example if you have a piece of information that makes sense as a content type - return it as field_content_type. That saves PHP a bit of work, and also makes it available on the frontend.
Will the settings eventually be available on the font end?
We’re not opposed to the idea and global settings might add a new perspective, but finding a genuine need for it has been difficult.
It would have to be something that can’t be added/modified when the field is saved and something that isn’t entirely appropriate as a tag parameter.
The rule of thumb we’ve followed for our own fields is that the tag alone outputs the data in as simple a format as possible and the developer can use tag parameters or tag pairs to modify the data to their needs.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.