StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
792 stars 61 forks source link

Modify a field inside a repeater field #141

Closed marcelo2605 closed 2 years ago

marcelo2605 commented 3 years ago

Basically I have a repeater field called items and a sub field called title and need to modify this title field. I tried this:

$accordionModule = new FieldsBuilder('accordion-module');

$accordionModule->addFields(get_field_partial('partials.partial-load-content'));

$accordionModule->modifyField('title', function($fieldsBuilder) {
    $fieldsBuilder->setConfig('wrapper', ['width' => '77%']);
    return $fieldsBuilder;
});

But it throw a Fatal error:

Field title not found.

stevep commented 3 years ago

What is the output of $accordionModule->build() at the point before you try to modify it?

marcelo2605 commented 3 years ago

@stevep here is:

[20-Aug-2021 21:35:12 UTC] Array
(
    [key] => group_accordion-module
    [title] => Accordion-module
    [fields] => Array
        (
            [0] => Array
                (
                    [type] => true_false
                    [name] => load_content
                    [label] => Load content?
                    [key] => field_accordion-module_load_content
                    [wrapper] => Array
                        (
                            [width] => 33%
                        )

                    [ui] => 1
                )

            [1] => Array
                (
                    [choices] => Array
                        (
                        )

                    [type] => select
                    [name] => select_post_type
                    [label] => Select Post Type
                    [key] => field_accordion-module_select_post_type
                    [required] => 1
                    [wrapper] => Array
                        (
                            [width] => 67%
                        )

                    [allow_null] => 1
                    [conditional_logic] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => field_accordion-module_load_content
                                            [operator] => ==
                                            [value] => 1
                                        )

                                    [1] => Array
                                        (
                                            [field] => field_accordion-module_select_posts
                                            [operator] => !=
                                            [value] => 1
                                        )

                                )

                        )

                )

            [2] => Array
                (
                    [type] => true_false
                    [name] => filter_taxonomy
                    [label] => Filter by taxonomy?
                    [key] => field_accordion-module_filter_taxonomy
                    [wrapper] => Array
                        (
                            [width] => 33%
                        )

                    [ui] => 1
                    [conditional_logic] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => field_accordion-module_load_content
                                            [operator] => ==
                                            [value] => 1
                                        )

                                    [1] => Array
                                        (
                                            [field] => field_accordion-module_select_posts
                                            [operator] => !=
                                            [value] => 1
                                        )

                                )

                        )

                )

            [3] => Array
                (
                    [choices] => Array
                        (
                        )

                    [type] => select
                    [name] => select_taxonomy
                    [label] => Select Taxonomy
                    [key] => field_accordion-module_select_taxonomy
                    [required] => 1
                    [wrapper] => Array
                        (
                            [width] => 33%
                        )

                    [allow_null] => 1
                    [conditional_logic] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => field_accordion-module_load_content
                                            [operator] => ==
                                            [value] => 1
                                        )

                                    [1] => Array
                                        (
                                            [field] => field_accordion-module_filter_taxonomy
                                            [operator] => ==
                                            [value] => 1
                                        )

                                )

                        )

                )

            [4] => Array
                (
                    [choices] => Array
                        (
                        )

                    [type] => select
                    [name] => select_term
                    [label] => Select Term
                    [key] => field_accordion-module_select_term
                    [required] => 1
                    [wrapper] => Array
                        (
                            [width] => 33%
                        )

                    [allow_null] => 1
                    [conditional_logic] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => field_accordion-module_load_content
                                            [operator] => ==
                                            [value] => 1
                                        )

                                    [1] => Array
                                        (
                                            [field] => field_accordion-module_filter_taxonomy
                                            [operator] => ==
                                            [value] => 1
                                        )

                                )

                        )

                )

            [5] => Array
                (
                    [type] => true_false
                    [name] => select_posts
                    [label] => Select posts?
                    [key] => field_accordion-module_select_posts
                    [ui] => 1
                    [conditional_logic] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => field_accordion-module_load_content
                                            [operator] => ==
                                            [value] => 1
                                        )

                                )

                        )

                )

            [6] => Array
                (
                    [type] => post_object
                    [name] => selected_posts
                    [label] => Selected Posts
                    [key] => field_accordion-module_selected_posts
                    [required] => 1
                    [post_type] => Array
                        (
                        )

                    [taxonomy] => Array
                        (
                        )

                    [multiple] => 1
                    [return_format] => id
                    [ui] => 1
                    [conditional_logic] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => field_accordion-module_load_content
                                            [operator] => ==
                                            [value] => 1
                                        )

                                    [1] => Array
                                        (
                                            [field] => field_accordion-module_select_posts
                                            [operator] => ==
                                            [value] => 1
                                        )

                                )

                        )

                )

            [7] => Array
                (
                    [type] => repeater
                    [name] => items
                    [label] => Items
                    [key] => field_accordion-module_items
                    [required] => 1
                    [min] => 1
                    [layout] => block
                    [button_label] => Add item
                    [conditional_logic] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => field_accordion-module_load_content
                                            [operator] => !=
                                            [value] => 1
                                        )

                                )

                        )

                    [sub_fields] => Array
                        (
                            [0] => Array
                                (
                                    [type] => text
                                    [name] => title
                                    [label] => Title
                                    [key] => field_accordion-module_items_title
                                    [required] => 1
                                )

                            [1] => Array
                                (
                                    [type] => wysiwyg
                                    [name] => description
                                    [label] => Description
                                    [key] => field_accordion-module_items_description
                                    [required] => 1
                                    [toolbar] => basic
                                    [media_upload] => 0
                                )

                        )

                )

        )

    [location] => 
)
stevep commented 3 years ago

I see, can you try to modify the repeater and then use that to modify the title? I think modify function will only work on a single level, it won't go sub fields.

marcelo2605 commented 3 years ago

@stevep you mean something like this?

$accordionModule->modifyField('items', function($fieldsBuilder) {
    $fieldsBuilder->modifyField('title', function($nestedFieldsBuilder) {
        $nestedFieldsBuilder->setConfig('wrapper', ['width' => '77%']);

        return $nestedFieldsBuilder;
    });

    return $fieldsBuilder;
});

It throw the same error: Fieldtitlenot found.

stevep commented 3 years ago

Yeah thats what I meant. Hmm.

To debug further what is the output for $fieldsBuilder->build(); once in the top level modifyField function?

marcelo2605 commented 3 years ago

@stevep here is:

[20-Aug-2021 22:08:30 UTC] Array
(
    [key] => group_
    [title] => 
    [fields] => Array
        (
            [0] => Array
                (
                    [type] => repeater
                    [name] => items
                    [label] => Items
                    [key] => field_field_items
                    [required] => 1
                    [min] => 1
                    [layout] => block
                    [button_label] => Add item
                    [conditional_logic] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [field] => load_content
                                            [operator] => !=
                                            [value] => 1
                                            [_field_does_not_exist] => load_content
                                        )

                                )

                        )

                    [sub_fields] => Array
                        (
                            [0] => Array
                                (
                                    [type] => text
                                    [name] => title
                                    [label] => Title
                                    [key] => field_field_items_title
                                    [required] => 1
                                )

                            [1] => Array
                                (
                                    [type] => wysiwyg
                                    [name] => description
                                    [label] => Description
                                    [key] => field_field_items_description
                                    [required] => 1
                                    [toolbar] => basic
                                    [media_upload] => 0
                                )

                        )

                )

        )

    [location] => 
)
stevep commented 3 years ago

I guess modify field can't currently be used on repeater / group fields which is what this old issue is talking about: https://github.com/StoutLogic/acf-builder/issues/82

Something to look at, there is an old pull request but it is lacking tests and since it was awhile ago, probably needs some to make sure it still works.

marcelo2605 commented 3 years ago

You mean this one, right? https://github.com/StoutLogic/acf-builder/pull/91. I tested on my end but it still returning an error. Will try to debug more.

stevep commented 2 years ago

This has been released in version 1.12.0