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

RFC / POC: Repeatable Settings Fields #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

RFC / POC: Repeatable Settings Fields #70

wants to merge 3 commits into from

Conversation

ericfernance
Copy link
Contributor

@ericfernance ericfernance commented Oct 18, 2016

Hi Tareq, thanks for writing this I found it really helped speed up creating a WP plugin recently for a client, and a heap easier to work with than the existing WP settings API!

This PR is really not right to merge, it's more a POC or discussion starter about introducing repeatables as field type. If it's of interest I will refine so it can be merged.

Overview

This PR introduces a new field type repeatable created exactly the same was as existing field types. A repeatable is simply a container for other fields of the standard field types that are in the children array of the repeatable. See below:

                    array(
                        'name'    => 'repeatable',
                        'label'   => __( 'A repeatable group', 'wpbstyledcalc' ),
                        'desc'    => __( 'A repetable group', 'wpbstyledcalc' ),
                        'type'    => 'repeatable',
                        'children' => array(
                            array(
                                'name'              => 'big_tolerance',
                                'label'             => __( 'Big Tolerances', 'wpbstyledcalc' ),
                                'desc'              => __( 'What value should we use for the larger tolerance', 'wpbstyledcalc' ),
                                'placeholder'       => __( 'Big Tolerance...', 'wpbstyledcalc' ),
                                'type'              => 'text',
                                'default'           => '25',
                            ),
                            array(
                                'name'              => 'small_tolerance',
                                'label'             => __( 'Small Tolerances', 'wpbstyledcalc' ),
                                'desc'              => __( 'What value should we use for the small tolerance', 'wpbstyledcalc' ),
                                'placeholder'       => __( 'Small Tolerance...', 'wpbstyledcalc' ),
                                'type'              => 'text',
                                'default'           => '25',
                            ),
                        )
                    )

This would show up in the settings page as:

2016-10-19_07-10-56

On the form submit the data is json encoded into a hidden input field that matches the top level name of the repeatable type. In this case repeatable.

On the form display the JSON encoded option is parsed and the data used to populate the children element array.

The Add button will add a new row and the X button will delete an existing row.

Work to Be Done

If there's interest in adding this feature the main work that still needs to be done is to move fields other than text into the repeatable when creating the child fields for the markup.

Currently the only field that works as a repeatable child is a text field

To do this properly I would probably slightly modify the existing callback_type methods to allow their use in the repeatable as well as stand alone fields.

I would also like to remove some of the inline javascript and move into the jquery.repeatable.js file.

@tareq1988
Copy link
Owner

Hello @digitalgarage, awesome work! The main goal of this class is to remain as simple as possible to provide settings solution. That's why I like to keep this into just a single file, no dependencies, just copy a single file and do the work.

While the idea of a repeatable field is great and mostly used in the custom field libraries, there might be some use of it for sure, but I am not sure bringing the repeatable field in the settings area is a good idea and keep the class in a single file as well.

That's what I feel, not sure what others think though.

@ericfernance
Copy link
Contributor Author

Hey @tareq1988 for sure I hear what you are saying!

I coded it mainly for a specific use case I have so I'll keep working on it anyway, since I need to make repeatables out of checkboxes and selects as well (and inline the JS). If there's enough interest then feel free to merge it in, if not that's cool too.

Eric.

@factmaven
Copy link

Great work @digitalgarage, I can see myself using this feature if it's ever implemented.

@manzoorwanijk
Copy link
Contributor

manzoorwanijk commented May 22, 2017

Repeatable fields are very useful in settings page as well. I'm developing a plugin where I need it. @digitalgarage did you manage to implement other field types in repeatable? If yes, can you please share the code?

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

Successfully merging this pull request may close these issues.

4 participants