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

Field name error #38

Closed
christianmagill opened this issue May 23, 2017 · 6 comments
Closed

Field name error #38

christianmagill opened this issue May 23, 2017 · 6 comments

Comments

@christianmagill
Copy link

I'm running into an issue with this code...

    $documents = new FieldsBuilder( 'documents');
    $documents
        ->addText('documents_title')
        ->addRepeater( 'documents', [
            'layout' => 'block',
        ])

ACF Builder seems to get confused when you are using the field group name as part of the field name. "documents_title" ends up getting the field name of "title".

screen shot 2017-05-23 at 9 54 04 am

@stevep
Copy link
Member

stevep commented May 23, 2017

@christianmagill is that the entire code you use to generate this field? Can you include the code used in the repeater as well?

The code as written currently yields the following config array on $documents->build(), which looks correct to me:

Array
(
    [key] => group_documents
    [title] => Documents
    [fields] => Array
        (
            [0] => Array
                (
                    [type] => text
                    [name] => documents_title
                    [label] => Documents Title
                    [key] => field_documents_documents_title
                )

            [1] => Array
                (
                    [type] => repeater
                    [name] => documents
                    [label] => Documents
                    [key] => field_documents_documents
                    [layout] => block
                    [button_label] => Add Document
                    [sub_fields] => Array
                        (
                        )

                )

        )

    [location] => 
)

I'm also wondering if this is ACF Builder or if ACF itself is "helping" when it detects the group name at the beginning of the field name and is striping it off. I've not run into that functionality before though.

@christianmagill
Copy link
Author

$documents = new FieldsBuilder( 'documents');
    $documents
        ->addText('documents_title')
        ->addRepeater( 'documents', [
            'layout' => 'block',
        ])
            ->addText('title')
            ->addFile('file')
            ->endRepeater()
        ->setLocation('post_type', '==', 'page');

    acf_add_local_field_group( $documents->build() );

@stevep
Copy link
Member

stevep commented May 23, 2017

Ok so that yields:

Array
(
    [key] => group_documents
    [title] => Documents
    [fields] => Array
        (
            [0] => Array
                (
                    [type] => text
                    [name] => documents_title
                    [label] => Documents Title
                    [key] => field_documents_documents_title
                )

            [1] => Array
                (
                    [type] => repeater
                    [name] => documents
                    [label] => Documents
                    [key] => field_documents_documents
                    [layout] => block
                    [button_label] => Add Document
                    [sub_fields] => Array
                        (
                            [0] => Array
                                (
                                    [type] => text
                                    [name] => title
                                    [label] => Title
                                    [key] => field_documents_documents_title
                                )

                            [1] => Array
                                (
                                    [type] => file
                                    [name] => file
                                    [label] => File
                                    [key] => field_documents_documents_file
                                )

                        )

                )

        )
)

So you can see we end up with two fields with the same key: field_documents_documents_title which is what your screenshot also says. There isn't really a way to avoid this. ACF Builder attempts to use a combination of the group name, field name and any ancestor field names to automatically build a "unique" field key.

I'm thinking I should add some sort of duplicate key exception maybe, so you'd be made aware of this right away.

If you removed the "documents_" string from the field name it would fix this issue:

$documents = new FieldsBuilder( 'documents');
$documents
    ->addText('title')
    ->addRepeater( 'documents', [
        'layout' => 'block',
    ])
    ->addText('title')
    ->addFile('file')
    ->endRepeater()
    ->setLocation('post_type', '==', 'page');

Yields:

Array
(
    [key] => group_documents
    [title] => Documents
    [fields] => Array
        (
            [0] => Array
                (
                    [type] => text
                    [name] => title
                    [label] => Title
                    [key] => field_documents_title
                )

            [1] => Array
                (
                    [type] => repeater
                    [name] => documents
                    [label] => Documents
                    [key] => field_documents_documents
                    [layout] => block
                    [button_label] => Add Document
                    [sub_fields] => Array
                        (
                            [0] => Array
                                (
                                    [type] => text
                                    [name] => title
                                    [label] => Title
                                    [key] => field_documents_documents_title
                                )

                            [1] => Array
                                (
                                    [type] => file
                                    [name] => file
                                    [label] => File
                                    [key] => field_documents_documents_file
                                )

                        )

                )
        )
)

Alternatively you could add a custom key to the title field in the repeater:

$documents = new FieldsBuilder( 'documents');
$documents
    ->addText('documents_title')
    ->addRepeater( 'documents', [
        'layout' => 'block',
    ])
    ->addText('title')
        ->setKey('document_title')
    ->addFile('file')
    ->endRepeater()

That yields:

Array
(
    [key] => group_documents
    [title] => Documents
    [fields] => Array
        (
            [0] => Array
                (
                    [type] => text
                    [name] => documents_title
                    [label] => Documents Title
                    [key] => field_documents_documents_title
                )

            [1] => Array
                (
                    [type] => repeater
                    [name] => documents
                    [label] => Documents
                    [key] => field_documents_documents
                    [layout] => block
                    [button_label] => Add Document
                    [sub_fields] => Array
                        (
                            [0] => Array
                                (
                                    [type] => text
                                    [name] => title
                                    [label] => Title
                                    [key] => field_documents_documents_document_title
                                )

                            [1] => Array
                                (
                                    [type] => file
                                    [name] => file
                                    [label] => File
                                    [key] => field_documents_documents_file
                                )

                        )

                )

        )

)

@christianmagill
Copy link
Author

Probably the alternative approach is the way to go. I don't want "title" at the root level to avoid clashing with other possible groups.

Are there character limits to how long a key can be? How about including "group" in the naming to differentiate?

@stevep
Copy link
Member

stevep commented May 23, 2017

I have run into an issue before when the SUHOISN PHP module is enabled and there is a limit on POST var names. See this stack overflow link

Maybe there is a better word than title for both, such as document name or document headline or maybe even document section title.

Or maybe staying consistent and inside the repeater and name those fields document_title and document_file, that would generate:

Array
(
    [key] => group_documents
    [title] => Documents
    [fields] => Array
        (
            [0] => Array
                (
                    [type] => text
                    [name] => documents_title
                    [label] => Documents Title
                    [key] => field_documents_documents_title
                )

            [1] => Array
                (
                    [type] => repeater
                    [name] => documents
                    [label] => Documents
                    [key] => field_documents_documents
                    [layout] => block
                    [button_label] => Add Document
                    [sub_fields] => Array
                        (
                            [0] => Array
                                (
                                    [type] => text
                                    [name] => document_title
                                    [label] => Document Title
                                    [key] => field_documents_documents_document_title
                                )

                            [1] => Array
                                (
                                    [type] => file
                                    [name] => document_file
                                    [label] => Document File
                                    [key] => field_documents_documents_document_file
                                )
                        )
                )
        )
)

@christianmagill
Copy link
Author

Cool, I see your point...

I probably need to revisit my naming conventions in general to stop these kind of issues.

Much thanks for your help!

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