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

ACF JS Api #10

Open
crtl opened this issue Nov 6, 2018 · 1 comment
Open

ACF JS Api #10

crtl opened this issue Nov 6, 2018 · 1 comment

Comments

@crtl
Copy link

crtl commented Nov 6, 2018

Hello,
your examples helped me a lot but I found the new ACF JS API which makes things just easy.
Given the following example, all fields (supporting up to one repeater) will be just appended to the fields-key in the request:

    acf.addFilter("select2_ajax_data", function(data, args, $input, field, instance) {
        console.log("select2_ajax_data", arguments);

        var fields = acf.getFields();
        var fieldValues = {};

        for (var fieldKey in fields) {
            if (!fields.hasOwnProperty(fieldKey)) continue;

            var field = fields[fieldKey];

            if (fieldValues.hasOwnProperty(field.data.name)) {
                if (!Array.isArray(fieldValues[field.data.name])) {
                    fieldValues[field.data.name] = [fieldValues[field.data.name]];
                }

                fieldValues[field.data.name].push(field.val());
            } else {
                fieldValues[field.data.name] = field.val();
            }
        }

        data.fields = fieldValues;

        return data;
    });
@Hube2
Copy link
Owner

Hube2 commented Nov 7, 2018

Yes, I have been playing with the new ACF JS API lately myself and the .val() method of the field helps with getting and setting values. If I have time to do new examples or update old examples I will probably incorporate it rather than work directly with field elements.

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

No branches or pull requests

2 participants