Conditional logic not working with clones in groups
Support › MB Conditional Logic › Conditional logic not working with clones in groups
- This topic has 12 replies, 2 voices, and was last updated 7 years, 1 month ago by
Anh Tran.
-
AuthorPosts
-
February 5, 2018 at 5:26 PM #8459
npp
Participant$prefix = '_something_'; $meta_boxes[] = array( 'title' => 'Box settings', 'post_types' => 'page', 'fields' => array( array( 'id' => $prefix . 'test', 'name' => 'Image or video boxes', 'type' => 'group', 'clone' => true, 'sort_clone' => true, 'collapsible' => true, 'group_title' => 'Box #{#}', 'save_state' => true, 'fields' => array( array( 'name' => 'Image or video?', 'id' => 'type', 'type' => 'radio', 'options' => [ 'img' => 'Image', 'vid' => 'Video', ], 'std' => 'img', 'inline' => false, ), array( 'id' => 'image', 'name' => 'Choose an image', 'type' => 'image_advanced', 'force_delete' => false, 'max_file_uploads' => 1, 'max_status' => false, 'image_size' => 'thumbnail', 'visible' => [ 'type', 'img' ], // <-- not working ), array( 'id' => 'video', 'name' => 'Choose a video', 'type' => 'video', 'force_delete' => false, 'max_file_uploads' => 1, 'max_status' => false, 'visible' => [ 'type', 'vid' ], // <-- not working ), ), ), ), );
Problem #1
The conditional logic is not working. This is probably due to it being a cloneable group and it can't lookup the right cloneable field. This is what it's looking for:
{"visible":{"when":[["type","=","img"]],"relation":"and"}}
when it should be looking for:
{"visible":{"when":[["_something_test[0][type]","=","img"]],"relation":"and"}}
Problem #2
When cloning the group, _something_test[X][type] is forgotten. If I set it to be "img" and clone the group, the previous box (#1) gets forgotten and "img" is no longer checked/selected.Question:
How do I use conditional logic in groups and have them save correctly?February 7, 2018 at 10:49 AM #8478Anh Tran
KeymasterHi,
I've just checked your code and found that the problem is the ID of the radio field, which is
type
. As this is the radio field, there are actually 2 inputs with the same name. The JavaScript tries to *guess* the field using this selector[name*="type"]
. It's ambiguous, and actually it returns *another WordPress field*, which is thepost_type
(a hidden input).I'd suggest using prefix for your field's ID (as stated in the documentation). It's a good way to keep the field ID/name unique, and thus make the selector works. I've tried changing the ID from
type
totype123
and it works.Please try and let me know how it goes.
February 7, 2018 at 4:30 PM #8484npp
ParticipantYes I understand what you're saying. But I have tried, and it is the same result. Try this code:
$prefix = '_something_'; $meta_boxes[] = array( 'title' => 'Box settings', 'post_types' => 'page', 'fields' => array( array( 'id' => $prefix . 'test', 'name' => 'Image or video boxes', 'type' => 'group', 'clone' => true, 'sort_clone' => true, 'collapsible' => true, 'default_state' => 'collapsed', 'group_title' => 'Box #{#}', 'save_state' => true, 'fields' => array( array( 'name' => 'Image or video?', 'id' => $prefix . 'type123', 'type' => 'radio', 'options' => [ 'img' => 'Image', 'vid' => 'Video', ], 'std' => 'img', 'inline' => false, ), array( 'id' => $prefix . 'image', 'name' => 'Choose an image', 'type' => 'image_advanced', 'force_delete' => false, 'max_file_uploads' => 1, 'max_status' => false, 'image_size' => 'thumbnail', 'visible' => [ $prefix . 'type', 'img' ], // <-- not working ), array( 'id' => $prefix . 'video', 'name' => 'Choose video', 'type' => 'video', 'force_delete' => false, 'max_file_uploads' => 1, 'max_status' => false, 'visible' => [ $prefix . 'type', 'vid' ], // <-- not working ), ), ), ), );
That should work, but it doesn't. The reason I didn't have a $prefix in there was because it's a cloned group, it should *know* better. _something_test[0][type] isn't post_type. But okay, if we don't want to add logic to handle that, at least it should work with prefixed fields. But it doesn't.
Problem 1 "sort of" disappears, the 'default_state' doesn't work and one of the two alternatives are visible on page load until you expand. Then it disappears. Then if you select "Image", set an image, and "clone", the setting is lost (see problem #2).
Don't know what to do. I'll show you what happens here:
https://drive.google.com/file/d/18lzVdnsD8FrKVUlYgJzDNcF1v4lUSVB7/view?usp=sharingFebruary 7, 2018 at 4:41 PM #8485npp
ParticipantHmm can't edit the post. So the code is currently:
$prefix = '_something_'; $meta_boxes[] = array( 'title' => 'Box settings', 'post_types' => 'page', 'fields' => array( array( 'id' => $prefix . 'test', 'name' => 'Image or video boxes', 'type' => 'group', 'clone' => true, 'sort_clone' => true, 'collapsible' => true, 'default_state' => 'collapsed', 'group_title' => 'Box #{#}', 'save_state' => true, 'fields' => array( array( 'name' => 'Image or video?', 'id' => $prefix . 'type123', 'type' => 'radio', 'options' => [ 'img' => 'Image', 'vid' => 'Video', ], 'std' => 'img', 'inline' => false, ), array( 'id' => $prefix . 'image', 'name' => 'Choose image', 'type' => 'image_advanced', 'force_delete' => false, 'max_file_uploads' => 1, 'max_status' => false, 'image_size' => 'thumbnail', 'visible' => [ $prefix . 'type123', 'img' ], // <-- not working ), array( 'id' => $prefix . 'video', 'name' => 'Choose video', 'type' => 'video', 'force_delete' => false, 'max_file_uploads' => 1, 'max_status' => false, 'visible' => [ $prefix . 'type123', 'vid' ], // <-- not working ), ), ), ), );
And the above video applies. As you can see, it does not function as intended. It resets the value when you clone as for radio, it shows image selection by default outside collapse although being in collapsed state by default.. Yeah I can't get this to work.
February 7, 2018 at 5:47 PM #8488Anh Tran
KeymasterOh, I see. My previous reply was for another problem (show/hide the image/video field when selecting an option). I'll check this bug and reply soon.
February 9, 2018 at 3:36 PM #8514npp
ParticipantAny updates on this issue? If you need me to try something, let me know.
February 12, 2018 at 2:50 PM #8531Anh Tran
KeymasterI found the problem. The Conditional Logic applies CSS (
display: block
) inline to the field, which overwrites the CSS of the Group extension (collapsed state). I've just updated the Group extension. Please try and let me know how it goes.February 12, 2018 at 8:44 PM #8532npp
ParticipantSorry to say, it still doesn't work as expected. The field is hidden now, and visible as it should be. But it still *forgets* when you hit clone.
This is still the code used to test with:
$prefix = '_something_'; $meta_boxes[] = array( 'title' => 'Box settings', 'post_types' => 'page', 'fields' => array( array( 'id' => $prefix . 'test', 'name' => 'Image or video boxes', 'type' => 'group', 'clone' => true, 'sort_clone' => true, 'collapsible' => true, 'default_state' => 'collapsed', 'group_title' => 'Box #{#}', 'save_state' => true, 'fields' => array( array( 'name' => 'Image or video?', 'id' => $prefix . 'type123', 'type' => 'radio', 'options' => [ 'img' => 'Image', 'vid' => 'Video', ], 'std' => 'img', 'inline' => false, ), array( 'id' => $prefix . 'image', 'name' => 'Choose image', 'type' => 'image_advanced', 'force_delete' => false, 'max_file_uploads' => 1, 'max_status' => false, 'image_size' => 'thumbnail', 'visible' => [ $prefix . 'type123', 'img' ], ), array( 'id' => $prefix . 'video', 'name' => 'Choose video', 'type' => 'video', 'force_delete' => false, 'max_file_uploads' => 1, 'max_status' => false, 'visible' => [ $prefix . 'type123', 'vid' ], ), ), ), ), );
This is a short video showcasing the issue at hand:
https://drive.google.com/file/d/1YKq_43Gz6hjl9FDMFI5PnTC20Z-fNtJd/view?usp=sharingAs you can see, as soon as I select "+ Add more", the previous is "forgotten".
February 13, 2018 at 10:43 AM #8539Anh Tran
KeymasterI got it. The problem is the cloning "radio" field. I've fixed it here. Please try and let me know how it goes.
Thanks
February 13, 2018 at 3:28 PM #8542npp
ParticipantAll right, I replaced the contents of
js/clone.js
in the main MetaBox plugin. So far, it does work in regards to not *forgetting* the previous box radio settings when cloning. However, one small issue still remains - it ignored the 'std' for the radio on clone.So in this case, I have
'std' => 'img'
and it's only respected for the first page load but not on clone. Seeing as they areradio
inputs, having one pre-filled is a pretty common feat in my opinion. It's either Image or Video, must be one or the other.Other than that, it works. Thank you!
February 13, 2018 at 10:05 PM #8545Anh Tran
KeymasterI got it. In this case, please add
'clone_default' => true
to the field and thestd
value will be cloned, too.February 13, 2018 at 10:31 PM #8549npp
ParticipantThank you. I somehow missed that entirely and that works splendidly!
Now everything works as I expected it to work. Thank you Anh for the support. Appreciate it! There's a bug though, not related to this but something I discovered while trying the fixes regarding this thread.
If you have an image module, upload an image, click the little 'pen' to edit the selected image and select 'delete permanently' the JS will throw an exception and the media.window or whatnot will bug out a bit and won't actually remove the image from the selection although it has been removed from the media library.
Just a smaller annoyance but still, worth mentioning.
February 22, 2018 at 10:27 AM #8591Anh Tran
KeymasterThanks for letting me know about that. Currently, handling removing images inside the media library is lack. It's kind of complicated. I will try to figure out from the Media Popup code later.
-
AuthorPosts
- The topic ‘Conditional logic not working with clones in groups’ is closed to new replies.