Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get the actual value of multiple_checkbox field type #40

Open
carlfromfelton opened this issue Apr 28, 2018 · 26 comments
Open

How to get the actual value of multiple_checkbox field type #40

carlfromfelton opened this issue Apr 28, 2018 · 26 comments

Comments

@carlfromfelton
Copy link

Hello.
Just a simple question. Can be a possibility to use a multiple_checkbox field type and get the actual values of the checked checkbox instead of a string like on||on|on|on|.
I am getting the values from a query and assign to the multiple_checkbox field on the options parameter. Is there any other field type that I can use for this purpose?
Thanks,

@elegantthemes elegantthemes deleted a comment from etstaging Apr 28, 2018
@lots0logs
Copy link
Member

lots0logs commented Apr 28, 2018

Hi, you can determine what each on/off corresponds to by using their index positions (they will always be in the same order as defined in the settings definition). Let me know if that helps 😃

@carlfromfelton
Copy link
Author

Hi, I try to do in the way you recommend but I am facing this issue now. Since externally can change the number of the checkboxes in the multiple_checkbox some items can change from checked to unchecked. This is a capture: https://www.dropbox.com/sh/9ti0dhb8wtpawej/AAAv_XsuhYBb_a7Vj5op2zNFa?dl=0

@lots0logs
Copy link
Member

lots0logs commented Apr 28, 2018

Ah, I see. Let me get back to you. I'm going to look into how we're currently dealing with category selection fields.

@xxtesaxx
Copy link

xxtesaxx commented Apr 28, 2018

Having the possibility to set a flag and get an array of the values which are checked rather than the on||on| list would be superb! I ran into this issue in 3.0.x and had to give up on an module idea. The only way to solve it would have been to write my own controls. Speaking about which, custom controls do not seem to work in the Visuabl Builder:
image

@carlfromfelton
Copy link
Author

Hi, @xxtesaxx I ran into the same issue and had to create my custom field too. But my custom field also is not fully supported within the VB. I know there is a field type named categories that use the checkboxes in another way so hopefully, will be a solution for us.

@xxtesaxx
Copy link

Yeah, a proper API to create new controls would be awesome! :)

@lots0logs
Copy link
Member

So, we're currently using the field type categories for this. However, I don't want to document it because its current API doesn't make much sense. Once we fix it, we'll document it. That being said, here is an example of how you can use it today:

'include_categories'   => array(
	'label'            => esc_html__( 'Include Categories', 'et_builder' ),
	'type'             => 'categories',
	'renderer_options' => array(
		'use_terms'    => true,
		'term_name'    => 'product_cat',  // This is actually the taxonomy slug
	),
	'description'      => esc_html__( 'Choose which categories you would like to include.', 'et_builder' ),
	'toggle_slug'      => 'main_content',
),

@xxtesaxx
Copy link

xxtesaxx commented Apr 28, 2018

I'm sure charles890123 already knows that and how to use it but that only works for terms of taxonomies :)

Imagine for example you want to let the user select post types or maybe some other value which the user can define somewhere in the backend. Once a new value is added, it might mess with the selection. For example you have the post types "Artworks" and "Paintings" and now you add "Books". "on|on" becomes "on|on|" but instead of Artworks and Paintings, you now have Artworks and Books selected.

That is why we would need a way to let the multiple checkboxes field (or another new control) return an array or comma separated list of slugs. Somthing like this:

'multi_select_test'   => array(
    'label'            => esc_html__( 'Select Multiple Boxes', 'et_builder' ),
    'toggle_slug'      => 'main_content',
    'type' => 'multiple_checkboxes',
    'options' => array(
        'hello' => esc_html__('Hello', 'et_builder'),
        'world' => esc_html__('World', 'et_builder'),
    ),
    'return_slugs' => true,
),

or this:

'multi_select_test'   => array(
    'label'            => esc_html__( 'Select Multiple Boxes', 'et_builder' ),
    'toggle_slug'      => 'main_content',
    'type' => 'checkboxes',
    'options' => array(
        'hello' => esc_html__('Hello', 'et_builder'),
        'world' => esc_html__('World', 'et_builder'),
    ),
),

And the return value would be one of those:

"hello,world"   
"hello|world"
array(hello, world)

Now when a new value is added, no matter where in the options list, it would still return those values which were previously selected - which also brings backwards compatibility when updating our own modules. Because even if my option list is not dynamic, I might want to add new values in the future and maybe I want to provide the user a good usability by sorting the options by their localized labels.

@lots0logs
Copy link
Member

Yeah, I agree that would make much more sense than how it works today. We're going to replace it with a more robust solution soon. Most likely at the same time we launch support for custom fields in the VB.

@carlfromfelton
Copy link
Author

Good Day @lots0logs Any updates with this.

@odoremieux
Copy link

Any updates?

@odoremieux
Copy link

Did you gave up on us?

@lots0logs
Copy link
Member

No news on this, sorry! It has gotten pushed back due to other priorities. It's still planned though.

@odoremieux
Copy link

Any time frame?

@lots0logs
Copy link
Member

No, I'm afraid not at this time. Sorry!

@mrkenng
Copy link

mrkenng commented Dec 4, 2019

Any good news on this? :D

@easterncoder
Copy link

Hate to bump an old thread but any updates on this?

@xxtesaxx
Copy link

Just today I saw that apparently we now can create our own custom fields by providing a react component. You can now easily implement your own multi-select. You could for example use select2 or simply use buttons and also style them however you desire.

@easterncoder
Copy link

@xxtesaxx yeah, i was advised to go that route as well.

@gonzalesc
Copy link

Just today I saw that apparently we now can create our own custom fields by providing a react component. You can now easily implement your own multi-select. You could for example use select2 or simply use buttons and also style them however you desire.

Hi. Do you know any link about this, please?

@xxtesaxx
Copy link

xxtesaxx commented Sep 6, 2021

Simply create a new divi extension with npx create-divi-extension and check the sample in includes/fields. It's just another react component but I believe the value is stored as plain text so what I usually do is store more complex values as stringified json

@xxtesaxx
Copy link

xxtesaxx commented Sep 6, 2021

I also recommend to console.log or debug the field in render() to see all the props you have available. you can pass a lot of stuff via the field definition from php to jsx

@aaronmeder
Copy link

I could also use a better multi checkbox field 🙋🏻‍♂️

@seebeen
Copy link

seebeen commented Oct 19, 2021

It is insane we don't have a proper way to handle dynamic lists in option fields, and we have to code our own react component for it.

@divibooster
Copy link

I know this thread is old, but there still doesn't seem to be a native solution for this and I couldn't find an example jsx component, so I wrote my own and posted it here:

Divi Custom Field Type: Multi-checkboxes with IDs

Hope it's of use to someone :)

@seebeen
Copy link

seebeen commented Jul 24, 2023

It is. I'll backport it to my plugin.

Thanks a lot, bratan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants