Support Forum
Support › Meta Box Group › Default values of hidden field in clone groupResolved
I'm creating an agenda builder, which uses Metabox, and the Groups, Tabs, and Conditional Logic extensions.
My basic setup is a set of 3 nested groups that can be cloned. It allows me to create an agenda with the following format (as an example):
Section
- Item
-- Notes
- Item
-- Notes
Section
- Item
-- Notes
Section
- Item
-- Notes
-- Notes
-- Notes
It's all working properly now except one thing I can't seem to figure out.
The entire agenda is stored as a serialized array in the database. I use another script to parse and process agendas for other features of the website.
For all of my "notes" fields I have created a hidden field called "processed". When my processing script processes an agenda it will mark the notes as "processed" by updating the value of that hidden field to "1" instead of "0".
So far, so good. I can create an agenda, run my processing script, it processes the agenda and updates the "processed" field of each processed note to "1".
But if I open the agenda now and use the "add" button to add another note, it clones the previous field including the hidden field with the value of "1". So the newly added item would be ignored by the processing script because it looks like it's already processed.
I have the "std" setting of the hidden field set to "0", and I've tried different combinations of the "clone_default" setting, but it seems no matter what I do the value of that hidden field is always "1" from the previous clone entry, instead of the default value of 0 that I'm trying to get.
Is there a way for me to set the default value of a hidden field in a nested clone group so that the default value is used when a new clone is added instead of using the value from the previous field?
I've solved this by watching for DOM changes and clearing the value of the field when a new clone is added.
I am facing the exact same problem.
After going through the code, I came to the conclusion, that it is currently not possible.
First of all, you cannot set a default of '0' (type=string). Internally the std value is checked against empty()
(see RWMB_Group_Field::child_field_std). Unfortunately, empty( '0' ) === true
.
Next, Metabox' client-side cloning completely ignores hidden fields (see https://github.com/wpmetabox/meta-box/blob/master/js/clone.js#L91-L102). The cloning is implemented by copying the HTML of the previous element and then applying some functions on the newly inserted fields. Usually, at that time, the default values are set.
I've implemented a solution to this issue that's working for me.
Cloning the group does clone my hidden fields, but they keep the same value.
I have some JavaScript that listens for changes to the DOM directly, and if a clone is added to that group the value is reset to 0.
I haven't tested it extensively yet, but in the testing I have done it seems to be working exactly how I want.