Giter Site home page Giter Site logo

Field name error about acf-builder HOT 6 CLOSED

stoutlogic avatar stoutlogic commented on August 21, 2024
Field name error

from acf-builder.

Comments (6)

stevep avatar stevep commented on August 21, 2024

@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.

from acf-builder.

christianmagill avatar christianmagill commented on August 21, 2024
$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() );

from acf-builder.

stevep avatar stevep commented on August 21, 2024

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
                                )

                        )

                )

        )

)

from acf-builder.

christianmagill avatar christianmagill commented on August 21, 2024

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?

from acf-builder.

stevep avatar stevep commented on August 21, 2024

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
                                )
                        )
                )
        )
)

from acf-builder.

christianmagill avatar christianmagill commented on August 21, 2024

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!

from acf-builder.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.